I have an object which stores two strings and overrides the ToString() method to return one of those strings
fooBar temp;
foreach (string foo in bar)
{
temp = new fooBar(); <--- Why is this line needed when I
am overwriting the code and path on each cycle before adding?
temp.code += ".";
temp.path += ".";
Console.WriteLine(temp);
clbFooBar.Items.Add(temp);
}
The prints to the console obey the overwritten code and path string values, but the checked list box items are all exactly the same thing (the last item's ToString()), what underlying process is going on that isn't immediately obvious?
If I change it to clbFooBar.Items.Add(temp.ToString()); it works absolutely fine, but wouldn't that be the same as temp.ToString.ToString() because .Add calls .ToString() in the first place? I figured both a console print and .Add would act the same seeing as they both call ToString()
EDIT: temp = new fooBar(); is the fix to my problem, I just want to know why this is the fix when intuition leads me to think that overwriting the two strings, code and path, should be enough and why without this re-initialisation, all of the items in the checked list box are the same.
temp = new fooBar();?