Dynamic creation of objects

gilgamesh

Newcomer
Joined
Apr 28, 2004
Messages
3
Hi,
Thanks for your time. I have a naive C# question about dynamic creation of objects.

MyClass a = new MyClass(); works

but

MyClass [] a = new MyClass[2]; does not...It does not give any errors but the value is null and when I try to invoke a method on the class it gives an exception {"Object reference not set to an instance of an object."}

Am I missing something simple?

Thanks again.
G
 
Objects and arrays

The second statement is allocating an array of length 2 for storing MyClass objects. The objects themselves must be instantiated individually:

Code:
MyClass[] a = new MyClass[2];

MyClass[0] = new MyClass();
MyClass[1] = new MyClass();

Good luck :cool:
 
Back
Top