473,385 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

chnage the Array Element

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[i].A = i; //<== here is the error
Array_Q[i].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
Nov 15 '05 #1
3 1495
sh <hs***@yahoo.com> wrote:
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[i] = new Q(); and it will work
Array_Q[i].A = i; //<== here is the error
Array_Q[i].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 - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #2
sh
Ok .. thank you Mr.Jon Skeet
I will try it ... and I will see

Thank you agin
-----Original Message-----
sh <hs***@yahoo.com> wrote:
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 startwith. It doesn't create a single instance of Q. You need to change theelement 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[i] = new Q(); and it will work
Array_Q[i].A = i; //<== here is the error
Array_Q[i].B = j;
}


<snip>

It's very important to know the difference between

reference types andvalue types - if Q had been a value type, it would have worked. Seehttp://www.pobox.com/~skeet/csharp/parameters.html and
http://www.pobox.com/~skeet/csharp/memory.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
.

Nov 15 '05 #3
Thanks, it is very useful for the beginner like me
"sh" <hs***@yahoo.com> wrote in message
news:03****************************@phx.gbl...
Ok .. thank you Mr.Jon Skeet
I will try it ... and I will see

Thank you agin
-----Original Message-----
sh <hs***@yahoo.com> wrote:
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[i] = new Q(); and it will work
Array_Q[i].A = i; //<== here is the error
Array_Q[i].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 - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
.

Nov 15 '05 #4

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

Similar topics

6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
3
by: engartte | last post by:
Hi all, Let consider the following solution: #include <stdio.h> int main () { int a, temp; int i; for(i=0; i<10; i++){
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
21
by: subramanian100in | last post by:
Suppose we have char array; C allows taking the address of array (ie &array is valid) though the element array cannot be accessed. Is this allowed for use in binary search method (as given...
7
by: bowlderster | last post by:
Hello,all. I want to get the array size in a function, and the array is an argument of the function. I try the following code. /*************************************** */ #include<stdio.h>...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
1
by: chiefychf | last post by:
I'm working on a school project and I am having a few issues... The program calls for three arrays a,b,c that have to be sorted, then compared to even or odd and stored in arrays d & e, then merge...
7
by: Christof Warlich | last post by:
Hi, the subject says it all: I need to instantiate an array of objects where each object "knows" its arrary index. So far, this is easy as long as index is not a compile-time constant: class ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.