Get/Set for List<string> in C#</string>
-
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.
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.
-
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;
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, makelst
in yourDefCompareProperty
classstatic
so that there is only one of them which all instances share. Or pass an instance of theDefCompareProperty
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
-
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.
-
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;
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.
-
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.
-
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, makelst
in yourDefCompareProperty
classstatic
so that there is only one of them which all instances share. Or pass an instance of theDefCompareProperty
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
-
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.
Luc always uses the force.
-
Luc always uses the force.
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.
-
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.
-
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.
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...
-
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...
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.