PC Review


Reply
Thread Tools Rate Thread

Class Arrays

 
 
trinitypete
Guest
Posts: n/a
 
      5th Dec 2003
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[i] = new CLASSA();
}

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

Thanks in advance, Pete.
 
Reply With Quote
 
 
 
 
Jan Tielens
Guest
Posts: n/a
 
      5th Dec 2003
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" <(E-Mail Removed)> schreef in bericht
news:0da501c3bb49$4a859120$(E-Mail Removed)...
> 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[i] = new CLASSA();
> }
>
> then I can access the member fields fine? Am I missing
> something?
>
> Thanks in advance, Pete.



 
Reply With Quote
 
Codemonkey
Guest
Posts: n/a
 
      5th Dec 2003
> 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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> schreef in bericht
> news:0da501c3bb49$4a859120$(E-Mail Removed)...
> > 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[i] = new CLASSA();
> > }
> >
> > then I can access the member fields fine? Am I missing
> > something?
> >
> > Thanks in advance, Pete.

>
>



 
Reply With Quote
 
Guest
Posts: n/a
 
      8th Dec 2003
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.
>
>
>"Jan Tielens" <(E-Mail Removed)> wrote in

message
>news:(E-Mail Removed)...
>> 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" <(E-Mail Removed)> schreef in bericht
>> news:0da501c3bb49$4a859120$(E-Mail Removed)...
>> > 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[i] = new CLASSA();
>> > }
>> >
>> > then I can access the member fields fine? Am I

missing
>> > something?
>> >
>> > Thanks in advance, Pete.

>>
>>

>
>
>.
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
arrays of a class Quibbles Microsoft C# .NET 2 28th Jun 2004 09:02 AM
Defining methods for a class that are accessible when declaring arrays of that class??? Bob Rock Microsoft Dot NET 7 3rd Jun 2004 02:21 PM
Defining methods for a class that are accessible when declaring arrays of that class??? Bob Rock Microsoft C# .NET 7 3rd Jun 2004 02:21 PM
Defining methods for a class that are accessible when declaring arrays of that class??? Bob Rock Microsoft Dot NET Framework 7 3rd Jun 2004 02:21 PM
Random class and arrays BlueOysterCult Microsoft C# .NET 3 16th Jan 2004 07:58 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:15 AM.