I’m new to c# and need help with lists and objects.
Severe edit as suggested..
what I really want to do is
List<Item> MyList = new List<Item>();
Item MyItem = new Item();
MyItem.Colour = "red";
MyList.Add(MyItem);
MyItem.Colour = "blue";
MyList.Add(MyItem);
MyItem.Colour = "white";
MyList.Add(MyItem);
And my list will now contain 3 elements – red, blue, white.
I think I can do this by:
Item MyItem = new Item();
MyItem.Colour = "red";
MyList.Add(MyItem);
MyIten = new Item();
MyItem.Colour = "blue";
MyItem = new Item();
MyList.Add(MyItem);
MyItem = new Item();
MyItem.Colour = "white";
MyList.Add(MyItem);
Again, this is simplified. With my real data there are a lot more elements.
I’ll try that out;
TIA – Andrew
Based on your description, I’m guessing you have a scoping problem. Your code should look something like this… pay particular attention to where each variable is set. Also, with these types of scenarios it is often helpful to create a static factory method called
Parsethat constructs the object from a string.}
One more time
HTH, but your question is a bit unclear.