Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# generics list of objects used as a property - can't add values

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?

like image 865
Nils Avatar asked May 07 '09 08:05

Nils


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.


1 Answers

Normally, collections are initialized by the parent object:

public List<Message> Messages {get; private set;}

public Batch() { // constructor
    Messages = new List<Message>();
}

Now it should work as expected. Note that if you are using XmlSerializer you'll need to keep the public set too...

In some ways, the long-hand property code is easier here:

private List<Message> messages = new List<Message>();
public List<Message> Messages { get {return messages; } }

(no messing with constructors, etc)

like image 99
Marc Gravell Avatar answered Oct 24 '22 02:10

Marc Gravell