I'm trying generics for the first time and am having a problem.
I have a dll that sends messages in batches
there is a "Message" class and a "Batch" class in that dll
on the batch class, I have some public properties
on of the batch class's public properties is a property called "Messages" which is a list of the "Message" class as follows:
public List<Message> Messages {get;set;}
Method 1
I then have a test exe where I want to set the properties on the "Batch" class as follows:
Batch myBatch = new Batch()
myBatch.Messages.Add(
new MyNameSpace.Message(txtToAddress.Text, txtMessage.Text));
When I run the app, I get:
"Object reference not set to an instance of an object."
Method 2
After playing around a bit, I see that I can successfully do the following in the test exe:
List<MyNameSpace.Message> myMessages = new List<MyNameSpace.Message>();
myBatch.Messages.Add(
new MyNameSpace.Message(txtToAddress.Text, txtMessage.Text));
myBatch.Messages = myMessages;
I'd like to get it working in the first way because other programmers will be using the dll and it seems more intutive to use the first approach.
What am I missing to get the first method to work?