chnage the Array Element

S

sh

Hi All..

i am Writing a C# code ..but i had face a small problem ,
i hope that you can solve it Please.

I had creat the following class

class Q
{
int A;
Int B
}

then in the main i creat an array of the above class

Q[] Array_Q = new Q[55]();

then i tried to fill the elemnets with a values

int i,j;
for (i=0 ; i<=10 ; i++)
{
for (j=i;j<=10;j++)
Array_Q.A = i; //<== here is the error
Array_Q.B = j;
}


if I run the program it give me an Exchption handelling
Error ..!!
so what is the Problem do you think..


thank you All
 
J

Jon Skeet

sh said:
then in the main i creat an array of the above class

Q[] Array_Q = new Q[55]();

That creates an array of references, all of which are null to start
with. It doesn't create a single instance of Q. You need to change the
element to refer to a valid instance before doing anything else.
then i tried to fill the elemnets with a values

int i,j;
for (i=0 ; i<=10 ; i++)
{
for (j=i;j<=10;j++)

Put in: Array_Q = new Q(); and it will work
Array_Q.A = i; //<== here is the error
Array_Q.B = j;
}


<snip>

It's very important to know the difference between reference types and
value types - if Q had been a value type, it would have worked. See
http://www.pobox.com/~skeet/csharp/parameters.html and
http://www.pobox.com/~skeet/csharp/memory.html
 
S

sh

Ok .. thank you Mr.Jon Skeet
I will try it ... and I will see

Thank you agin
-----Original Message-----
sh said:
then in the main i creat an array of the above class

Q[] Array_Q = new Q[55]();

That creates an array of references, all of which are null to start
with. It doesn't create a single instance of Q. You need to change the
element to refer to a valid instance before doing anything else.
then i tried to fill the elemnets with a values

int i,j;
for (i=0 ; i<=10 ; i++)
{
for (j=i;j<=10;j++)

Put in: Array_Q = new Q(); and it will work
Array_Q.A = i; //<== here is the error
Array_Q.B = j;
}


<snip>

It's very important to know the difference between reference types and
value types - if Q had been a value type, it would have worked. See
http://www.pobox.com/~skeet/csharp/parameters.html and
http://www.pobox.com/~skeet/csharp/memory.html

--
Jon Skeet - <[email protected]>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
.
 
D

David Lau

Thanks, it is very useful for the beginner like me
sh said:
Ok .. thank you Mr.Jon Skeet
I will try it ... and I will see

Thank you agin
-----Original Message-----
sh said:
then in the main i creat an array of the above class

Q[] Array_Q = new Q[55]();

That creates an array of references, all of which are null to start
with. It doesn't create a single instance of Q. You need to change the
element to refer to a valid instance before doing anything else.
then i tried to fill the elemnets with a values

int i,j;
for (i=0 ; i<=10 ; i++)
{
for (j=i;j<=10;j++)

Put in: Array_Q = new Q(); and it will work
Array_Q.A = i; //<== here is the error
Array_Q.B = j;
}


<snip>

It's very important to know the difference between reference types and
value types - if Q had been a value type, it would have worked. See
http://www.pobox.com/~skeet/csharp/parameters.html and
http://www.pobox.com/~skeet/csharp/memory.html

--
Jon Skeet - <[email protected]>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
.
 

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