473,503 Members | 12,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class Arrays

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.
Jul 21 '05 #1
3 1204
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" <su*****@trinity.com> schreef in bericht
news:0d****************************@phx.gbl...
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.

Jul 21 '05 #2
> 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" <ja*@no.spam.please.leadit.be> wrote in message
news:uG**************@TK2MSFTNGP10.phx.gbl...
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" <su*****@trinity.com> schreef in bericht
news:0d****************************@phx.gbl...
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.


Jul 21 '05 #3
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 themto be initialized to a set of values other than 0.

Besides, when you create an array of CLASSA objects, you can also also storeobjects that inherit from CLASSA in it. How does the framework know whattype 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" <ja*@no.spam.please.leadit.be> wrote in messagenews:uG**************@TK2MSFTNGP10.phx.gbl...
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" <su*****@trinity.com> schreef in bericht
news:0d****************************@phx.gbl...
> 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.


.

Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
3145
by: Dan | last post by:
Hello, I have trouble with class calling. I am calling getvolume() with succes in the function CreateCircle but it do not want to call it in ShowCircle() function. I am staying in the same...
2
2236
by: David | last post by:
Hi all, I am fairly new to C#. so go easy on me :-) Anyhow, I have a class file that I have set up properties and a method. I am calling this class file directly from and aspx.cs file. So...
1
2232
by: stromhau | last post by:
Hi, I have made a few classes in c++. They somehow cooperate doing some 3d stuff. Basically it is a moving camera acting as a flight, i have placed a lot of objects around the scene together with...
12
2161
by: mast2as | last post by:
Hi everyone I am working on some code that uses colors. Until recently this code used colors represented a tree floats (RGB format) but recently changed so colors are now defined as spectrum....
6
3540
by: Marvin Barley | last post by:
I have a class that throws exceptions in new initializer, and a static array of objects of this type. When something is wrong in initialization, CGI program crashes miserably. Debugging shows...
0
7193
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5562
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4992
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.