Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

defining an array of type of a class

Status
Not open for further replies.

VBdevil

Programmer
Sep 19, 2002
75
IL
Hi,

I have an application in which I have some general definitions that I need to use in different classes in the application.

What I did was create a class named Definitions, that looks like this, for example:

public class Definitions
{
public string id1;
public string id2;
.
.
.
}

I then wanted to define a variable of the type of this class, for example:
Definitions defs = new Definitions();

But I want this variable to be an array, so I wrote:
Definitions[] defs = new Definitions[10];

But when I used this array in a function it threw a NullReferenceException, saying: "Object reference not set to an instance of an object"...

I don't understand why this happens..
did I not define the array of type of the class correctly?
Is it not possible to define an array of type class?

Thanks for your help!
 
That error is normal:
Definitions[] defs = new Definitions[10];
The above statement declares an array of 1o pointers to the Definitions objects. So, there is no any memory allocation.
You must allocate memory before trying to access an element of that array.
Example:
Code:
defs[0]=new Definitions();
defs[1]=new Definitions();
Now, you can access the members of the defs[0], or defs[1] objects.
obislavu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top