1

I used to have in my code the following class

public class SomeListClass
{ 
       public List<Boolean> ListA {get; set;}
       public List<Boolean> ListB {get; set;}

       public List<String> ListStringsA {get; set;}
       public List<String> ListStringsB {get; set;}
       public List<String> ListStringsC {get; set;}
       public List<String> ListStringsD {get; set;}

       public void InitLists()
       {
            ListStringsA  = ListStringsB = ListStringsC = ListStringsD = new List<String>();
            ListA = ListB = new List<Boolean>();
       }
}

What did I notice using this initialisation and the following piece of code:

SomeListClass itm = new SomeListClass();
itm.InitLists();
itm.ListStringsA.Add("TestString");
itm.ListA.Add(true);
  • If I add a String to ListStringsA, then it gets only added to ListStringsA
  • If I add a Boolean to ListA then it get automatically added to ListB

So the result would be:

ListStringsA = {"TestString"}
ListStringsB = {}
ListStringsC = {}
ListStringsD = {}
ListA  = {true}
ListB = {true}

What is the reason herefor? Could someone point me some to some documentation explaining this odd behavior? I can't seem to find anything.

Note: This solved it:

   public void InitLists()
   {
        ListStringsA = ListStringsB = ListStringsC = ListStringsD = new List<String>();
        ListA = new List<Boolean>();
        ListB = new List<Boolean>();
   }

Note: Some simplyfied production code:

I get a list of type SomeListObject (see Below) named objectList

public class SomeListObject {
    public String SomeListObjectName {get; set;}
    public Boolean SomeListObjectNeeded {get; set;}
    public Boolean SomeListObjectOk {get; set;}
}

The list contains 2 items:

objectList = {{SomeListObjectName  = "PersonA", SomeListObjectNeeded  = true, SomeListObjectOk = false},{SomeListObjectName  = "PersonB", SomeListObjectNeeded  = true, SomeListObjectOk = false}}

Then I do a foreach:

SomeListClass itm = new SomeListClass();
itm.InitLists();
foreach (SomeListObject  myObj in objectList )
{
      ListStringsA.Add(myObj.SomeListObjectName);
      // ListStringsA: {"PersonA"}
      // ListStringsB: {}
      // ListA: {}
      // ListB: {}
      ListA.Add(myObj.SomeListObjectNeeded);
      // ListStringsA: {"PersonA"}
      // ListStringsB: {}
      // ListA: {true}
      // ListB: {true}
      ListB.Add(myObj.SomeListObjectNeeded);
      // ListStringsA: {"PersonA"}
      // ListStringsB: {}
      // ListA: {true, false}
      // ListB: {true, false} <=== Say What now???

}
User999999
  • 2,500
  • 7
  • 37
  • 63

3 Answers3

5

This happens because you're creating only one list for bool and string, then just assigning it to variable. See more.

If you want to create new list for each property, see this:

ListStringsA = new List<string>(); ListStringsB = new List<string>(); ListStringsC = new List<string>(); ListA = new List<bool>(); ListB = new List<bool>();

Community
  • 1
  • 1
Erti-Chris Eelmaa
  • 25,338
  • 6
  • 61
  • 78
  • Unfortunatly this is not the case. I would expect the same behavior your're displaying. Except it doesn't. – User999999 Feb 18 '15 at 08:39
  • @User999999: Are you saying that if you put the code you've given in post to your new project (without any additions), it doesn't give you the expected output? Don't forget that there is no point to talk about this problem if you're actually comparing some other code, instead the **EXACT** one you have in the post. – Erti-Chris Eelmaa Feb 18 '15 at 08:43
  • I have supplied some extra code in the question. It is as a matter of fact the productioncode only the names of the properties have been changed. – User999999 Feb 18 '15 at 08:50
3

Are you sure?? I suppose that the result should be

ListStringsA = {"TestString"}
ListStringsB = {"TestString"}
ListStringsC = {"TestString"}
ListStringsD = {"TestString"}
ListA  = {true}
ListB = {true}
Galma88
  • 2,398
  • 6
  • 29
  • 50
1

Try for youreself

SomeList mysomelist = new SomeList();

mysomelist.InitLists();

mysomelist.ListA.Add(true);
mysomelist.ListStringsA.Add("a");

Console.WriteLine(mysomelist.ListA[0] + "   " + mysomelist.ListB[0]);
Console.WriteLine(mysomelist.ListStringsA[0] + "   " + mysomelist.ListStringsB[0] + "  " + mysomelist.ListStringsC[0] + "  " + mysomelist.ListStringsD[0]);

Output :

True    True 
a    a    a    a

Edit : may be you are trying something other than what's stated in the question

Kavindu Dodanduwa
  • 12,193
  • 3
  • 33
  • 46