Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Get/Set for List<string> in C#</string>

Get/Set for List<string> in C#</string>

Scheduled Pinned Locked Moved C#
csharphelp
13 Posts 6 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    ipstefan
    wrote on last edited by
    #1

    Hi guys. I got a problem I cant find solution to anywhere over the internet..I might have missed something in my logic. <pre>       class DefComparing:DefCompareProperty       {             public void DefCompare()             {                   List<string> def_compare = new List<string>();                   ********doing something to the list here****** DefCompareProperty df = new DefCompareProperty();                   foreach (string s in def_compare)                   {                         df.SetDefComp(s);                   }             }       } </pre> Then I want to use the def_compare list in another class. <pre> public class Aliniere       {             public List<string> def_compare = new List<string>();             public void Aliniate()             {                ******doing some operations here****                DefComparing newdefcomp = new DefComparing();                newdefcomp.DefCompare();                def_compare = df.GetDefComp();   //Trying to get the list from that other class </pre> What I tried to do is make Set/Get properties for the list. <pre>       class DefCompareProperty       {             private List<string> lst = new List<string>();             public List<string> GetDefComp()             {                   return lst;

    M A L 3 Replies Last reply
    0
    • I ipstefan

      Hi guys. I got a problem I cant find solution to anywhere over the internet..I might have missed something in my logic. <pre>       class DefComparing:DefCompareProperty       {             public void DefCompare()             {                   List<string> def_compare = new List<string>();                   ********doing something to the list here****** DefCompareProperty df = new DefCompareProperty();                   foreach (string s in def_compare)                   {                         df.SetDefComp(s);                   }             }       } </pre> Then I want to use the def_compare list in another class. <pre> public class Aliniere       {             public List<string> def_compare = new List<string>();             public void Aliniate()             {                ******doing some operations here****                DefComparing newdefcomp = new DefComparing();                newdefcomp.DefCompare();                def_compare = df.GetDefComp();   //Trying to get the list from that other class </pre> What I tried to do is make Set/Get properties for the list. <pre>       class DefCompareProperty       {             private List<string> lst = new List<string>();             public List<string> GetDefComp()             {                   return lst;

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      i would say in your SetDefComp you need to simply pass it the list and assign that to lst but that shouldnt really make to much difference with the code you have given. Are you getting any error messages? or is the list just empty?

      Life goes very fast. Tomorrow, today is already yesterday.

      I 1 Reply Last reply
      0
      • M musefan

        i would say in your SetDefComp you need to simply pass it the list and assign that to lst but that shouldnt really make to much difference with the code you have given. Are you getting any error messages? or is the list just empty?

        Life goes very fast. Tomorrow, today is already yesterday.

        I Offline
        I Offline
        ipstefan
        wrote on last edited by
        #3

        When I try to make foreach(string s in def_compare)          Console.Writeline(s); It doesnt write anything.I guess the list is empty. I tried how you told me to: in class 1: DefCompareProperty df = new DefCompareProperty(); df.SetDefComp(def_compare); in class 3: public void SetDefComp(List<string> members) { lst = members; } And I still have an empty list in class 2.

        M 1 Reply Last reply
        0
        • I ipstefan

          Hi guys. I got a problem I cant find solution to anywhere over the internet..I might have missed something in my logic. <pre>       class DefComparing:DefCompareProperty       {             public void DefCompare()             {                   List<string> def_compare = new List<string>();                   ********doing something to the list here****** DefCompareProperty df = new DefCompareProperty();                   foreach (string s in def_compare)                   {                         df.SetDefComp(s);                   }             }       } </pre> Then I want to use the def_compare list in another class. <pre> public class Aliniere       {             public List<string> def_compare = new List<string>();             public void Aliniate()             {                ******doing some operations here****                DefComparing newdefcomp = new DefComparing();                newdefcomp.DefCompare();                def_compare = df.GetDefComp();   //Trying to get the list from that other class </pre> What I tried to do is make Set/Get properties for the list. <pre>       class DefCompareProperty       {             private List<string> lst = new List<string>();             public List<string> GetDefComp()             {                   return lst;

          A Offline
          A Offline
          Anthony Mushrow
          wrote on last edited by
          #4

          It doesn't work because your creating a new instance of DefCompareProperty. Each new instance has its own List and they are kept completely separate. You can do this two ways, make lst in your DefCompareProperty class static so that there is only one of them which all instances share. Or pass an instance of the DefCompareProperty class to each of your other classes to use so that they both use the same one. EDIT: If you are passing the same instance around already then damn, I don't know what's going on.

          My current favourite word is: Delicious!

          -SK Genius

          Game Programming articles start -here[^]-

          I 1 Reply Last reply
          0
          • I ipstefan

            When I try to make foreach(string s in def_compare)          Console.Writeline(s); It doesnt write anything.I guess the list is empty. I tried how you told me to: in class 1: DefCompareProperty df = new DefCompareProperty(); df.SetDefComp(def_compare); in class 3: public void SetDefComp(List<string> members) { lst = members; } And I still have an empty list in class 2.

            M Offline
            M Offline
            musefan
            wrote on last edited by
            #5

            do some debugging in your code then... Put a message box in to show the length of the list before you try to write it...

            System.Windows.Forms.MessageBox.Show(def_compare.Length.ToString());

            Life goes very fast. Tomorrow, today is already yesterday.

            1 Reply Last reply
            0
            • I ipstefan

              Hi guys. I got a problem I cant find solution to anywhere over the internet..I might have missed something in my logic. <pre>       class DefComparing:DefCompareProperty       {             public void DefCompare()             {                   List<string> def_compare = new List<string>();                   ********doing something to the list here****** DefCompareProperty df = new DefCompareProperty();                   foreach (string s in def_compare)                   {                         df.SetDefComp(s);                   }             }       } </pre> Then I want to use the def_compare list in another class. <pre> public class Aliniere       {             public List<string> def_compare = new List<string>();             public void Aliniate()             {                ******doing some operations here****                DefComparing newdefcomp = new DefComparing();                newdefcomp.DefCompare();                def_compare = df.GetDefComp();   //Trying to get the list from that other class </pre> What I tried to do is make Set/Get properties for the list. <pre>       class DefCompareProperty       {             private List<string> lst = new List<string>();             public List<string> GetDefComp()             {                   return lst;

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Hi, you got it rather twisted. Basically what you have is: 1. a base class DefCompareProperty which holds a list that you can add to, and get. 2. a derived class DefComparing which inherits from DefCompareProperty, so it too holds the list DefCompareProperty is holding. 3. but then, inside the DefCompare method, you have the line DefCompareProperty df = new DefCompareProperty(); which adds a DefCompareProperty inside the DefComparing object (which is a DefCompareProperty itself), so now DefCompareProperty is actually holding two lists (until DefCompare method is done). Then you add items to the second list, and the method ends, so the second list no longer exists, and it all doesn't make much sense. So, you are getting everyone confused by using very similar class names; also the SetDefComp() method is badly named, it should reallybe called add since it does not set anything, it adds something. I would do things differently, something along these lines (not tested):

              // a list of strings, we will use the List methods, no need to add any code
              public class DefCompareProperty : List<string> {}

              public class DefComparing:DefCompareProperty {
              public void DefCompare() {
              this.Clear(); // clear the list
              // doing something to the list here using this.Add(...)
              }
              }

              The main differences are: 1. my classes don't hold lists, they are lists. 2. I operate on the existing list, not on a new one. Of course you could as well forego class DefCompareProperty and simply write public class DefComparing:List<string> {...} :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


              M 1 Reply Last reply
              0
              • L Luc Pattyn

                Hi, you got it rather twisted. Basically what you have is: 1. a base class DefCompareProperty which holds a list that you can add to, and get. 2. a derived class DefComparing which inherits from DefCompareProperty, so it too holds the list DefCompareProperty is holding. 3. but then, inside the DefCompare method, you have the line DefCompareProperty df = new DefCompareProperty(); which adds a DefCompareProperty inside the DefComparing object (which is a DefCompareProperty itself), so now DefCompareProperty is actually holding two lists (until DefCompare method is done). Then you add items to the second list, and the method ends, so the second list no longer exists, and it all doesn't make much sense. So, you are getting everyone confused by using very similar class names; also the SetDefComp() method is badly named, it should reallybe called add since it does not set anything, it adds something. I would do things differently, something along these lines (not tested):

                // a list of strings, we will use the List methods, no need to add any code
                public class DefCompareProperty : List<string> {}

                public class DefComparing:DefCompareProperty {
                public void DefCompare() {
                this.Clear(); // clear the list
                // doing something to the list here using this.Add(...)
                }
                }

                The main differences are: 1. my classes don't hold lists, they are lists. 2. I operate on the existing list, not on a new one. Of course you could as well forego class DefCompareProperty and simply write public class DefComparing:List<string> {...} :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                M Offline
                M Offline
                musefan
                wrote on last edited by
                #7

                No why couldn't the OP make it sound as simple as that... good work Luc ;)

                Life goes very fast. Tomorrow, today is already yesterday.

                P 1 Reply Last reply
                0
                • A Anthony Mushrow

                  It doesn't work because your creating a new instance of DefCompareProperty. Each new instance has its own List and they are kept completely separate. You can do this two ways, make lst in your DefCompareProperty class static so that there is only one of them which all instances share. Or pass an instance of the DefCompareProperty class to each of your other classes to use so that they both use the same one. EDIT: If you are passing the same instance around already then damn, I don't know what's going on.

                  My current favourite word is: Delicious!

                  -SK Genius

                  Game Programming articles start -here[^]-

                  I Offline
                  I Offline
                  ipstefan
                  wrote on last edited by
                  #8

                  I went for static in get/set class and it worked fine Thanks very much for the easy solution SK Genius. Thanks to all for trying to help too. Cya

                  1 Reply Last reply
                  0
                  • M musefan

                    No why couldn't the OP make it sound as simple as that... good work Luc ;)

                    Life goes very fast. Tomorrow, today is already yesterday.

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    Luc always uses the force.

                    L 1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      Luc always uses the force.

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      The Force as I see it consists of a few very simple rules. Sort of ten commandments. One of them is: if it does not work, and/or you don't understand it (isn't that almost the same?) try making it simpler. Simpler code means better understanding AND fewer bugs (and often better performance too). another one is: use proper names for everything, it really matters. :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                      M P 2 Replies Last reply
                      0
                      • L Luc Pattyn

                        The Force as I see it consists of a few very simple rules. Sort of ten commandments. One of them is: if it does not work, and/or you don't understand it (isn't that almost the same?) try making it simpler. Simpler code means better understanding AND fewer bugs (and often better performance too). another one is: use proper names for everything, it really matters. :)

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                        M Offline
                        M Offline
                        musefan
                        wrote on last edited by
                        #11

                        I will be looking forward to you article with the full 10... Also perhaps a bible of some sort so we can have a religion that follows logic (maybe a little fuzzy logic too ;) )

                        Life goes very fast. Tomorrow, today is already yesterday.

                        1 Reply Last reply
                        0
                        • L Luc Pattyn

                          The Force as I see it consists of a few very simple rules. Sort of ten commandments. One of them is: if it does not work, and/or you don't understand it (isn't that almost the same?) try making it simpler. Simpler code means better understanding AND fewer bugs (and often better performance too). another one is: use proper names for everything, it really matters. :)

                          Luc Pattyn [Forum Guidelines] [My Articles]


                          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                          P Offline
                          P Offline
                          Paddy Boyd
                          wrote on last edited by
                          #12

                          Luc Pattyn wrote:

                          another one is: use proper names for everything, it really matters.

                          I like this one. Now we just need to get some way to bring eternal damnation into play if people don't follow your commandments... Hmm...

                          L 1 Reply Last reply
                          0
                          • P Paddy Boyd

                            Luc Pattyn wrote:

                            another one is: use proper names for everything, it really matters.

                            I like this one. Now we just need to get some way to bring eternal damnation into play if people don't follow your commandments... Hmm...

                            L Offline
                            L Offline
                            Luc Pattyn
                            wrote on last edited by
                            #13

                            Paddy Boyd wrote:

                            Now we just need to get some way to bring eternal damnation into play...

                            Not really. IMO not obeying such rules makes programming and debugging harder, self-inflicted punishment is provided automatically. :)

                            Luc Pattyn [Forum Guidelines] [My Articles]


                            The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups