Class Arrays

T

trinitypete

Hi,

This post is just to check that I am not missing a point
somewhere.

I have a class say 'CLASSA' with a string member field
and associated property. I need an array of this class so
I execute the following code(C#)

CLASSA myclassarray = new CLASSA[5];

If I try to access the first elements memeber field
i.e. myclassarray.memberfield = "Hello";
I get an error saying object not set to instance of an
object. I thought this notation would create the complete
class array as the framework knows that each member of
the array is an instance of CLASSA.

If I initialise each element of the class array as
follows:

for ......
{
myclassarray = new CLASSA();
}

then I can access the member fields fine? Am I missing
something?

Thanks in advance, Pete.
 
J

Jan Tielens

The following line only creates an array instance:
CLASSA myclassarray = new CLASSA[5];

So at this point there are not instances in the array. You'll have to add
new instances to the array, and a the way you showed is a possibility.
 
C

Codemonkey

So at this point there are not instances in the array.

I agree. It's a bit like decalring an array of integers and expecting them
to be initialized to a set of values other than 0.

Besides, when you create an array of CLASSA objects, you can also also store
objects that inherit from CLASSA in it. How does the framework know what
type you actually want to store?

If you want to initialize instances in an array you can use the following:
(it's VB, I'm not sure of the C# eqiuvelent is)

------------------------
Private Function GetArray() as CLASSA()

' Returns an array of 3 new instances of CLASS A
return new CLASSA() {new CLASSA(), new CLASSA(), new CLASSA()}

End Function
------------------------

Hope this helps,

Trev.


Jan Tielens said:
The following line only creates an array instance:
CLASSA myclassarray = new CLASSA[5];

So at this point there are not instances in the array. You'll have to add
new instances to the array, and a the way you showed is a possibility.


--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan

trinitypete said:
Hi,

This post is just to check that I am not missing a point
somewhere.

I have a class say 'CLASSA' with a string member field
and associated property. I need an array of this class so
I execute the following code(C#)

CLASSA myclassarray = new CLASSA[5];

If I try to access the first elements memeber field
i.e. myclassarray.memberfield = "Hello";
I get an error saying object not set to instance of an
object. I thought this notation would create the complete
class array as the framework knows that each member of
the array is an instance of CLASSA.

If I initialise each element of the class array as
follows:

for ......
{
myclassarray = new CLASSA();
}

then I can access the member fields fine? Am I missing
something?

Thanks in advance, Pete.

 
G

Guest

Makes Sense - just checking I wasn't missing something.

Thanks. Pete.
-----Original Message-----
So at this point there are not instances in the array.

I agree. It's a bit like decalring an array of integers and expecting them
to be initialized to a set of values other than 0.

Besides, when you create an array of CLASSA objects, you can also also store
objects that inherit from CLASSA in it. How does the framework know what
type you actually want to store?

If you want to initialize instances in an array you can use the following:
(it's VB, I'm not sure of the C# eqiuvelent is)

------------------------
Private Function GetArray() as CLASSA()

' Returns an array of 3 new instances of CLASS A
return new CLASSA() {new CLASSA(), new CLASSA(), new CLASSA()}

End Function
------------------------

Hope this helps,

Trev.


The following line only creates an array instance:
CLASSA myclassarray = new CLASSA[5];

So at this point there are not instances in the array. You'll have to add
new instances to the array, and a the way you showed is a possibility.


--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan

trinitypete said:
Hi,

This post is just to check that I am not missing a point
somewhere.

I have a class say 'CLASSA' with a string member field
and associated property. I need an array of this class so
I execute the following code(C#)

CLASSA myclassarray = new CLASSA[5];

If I try to access the first elements memeber field
i.e. myclassarray.memberfield = "Hello";
I get an error saying object not set to instance of an
object. I thought this notation would create the complete
class array as the framework knows that each member of
the array is an instance of CLASSA.

If I initialise each element of the class array as
follows:

for ......
{
myclassarray = new CLASSA();
}

then I can access the member fields fine? Am I missing
something?

Thanks in advance, Pete.




.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top