Array initialization in class

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,
I have an array defined in a class. The type of the array is the class type.
For example:

class MyClass
{
private int a;
private int b;
private MyClass[] Array;
public MyClass()
{
a = 5;
b = 2;
Array = new MyClass[3];
}

public void SetArrayFields()
{
Array[0].a = 1;
Array[0].b = 8;
}
}

What happens is that whenever I enter SetArrayFields() and try to set values
to the array there is an error "System.NullReferenceException"

Please help me to solve this problem
Many thanks
 
Hi all,
I have an array defined in a class. The type of the array is the class type.
For example:

class MyClass
{
private int a;
private int b;
private MyClass[] Array;
public MyClass()
{
a = 5;
b = 2;
Array = new MyClass[3];
}

public void SetArrayFields()
{
Array[0].a = 1;
Array[0].b = 8;
}
}

What happens is that whenever I enter SetArrayFields() and try to set values
to the array there is an error "System.NullReferenceException"

Please help me to solve this problem
Many thanks

If you look at this with the debugger you will see that all elements
of the array are null.

The line:
Array = new MyClass[3];
allocates an array with all members set to null. You need to create
new instances of your class and store them in the array.

Besides that, your whole structure seems weird to me. Each instance
of your class contains an array of three instances of your class. Does
each of those instances also include 3 more instances? If so, your
class will occupy an infinite amount of memory.
 
Thanks, Jack
What do you suggest, then?
Should I make another class with an array of type MyClass and a public
method of inserting another element to the array.
And in the class MyClass - in the constructor, also to call that method with
the element that has just been created.
How does it sound?

Or maybe it's better to use a list?
Thanks

Jack Jackson said:
Hi all,
I have an array defined in a class. The type of the array is the class type.
For example:

class MyClass
{
private int a;
private int b;
private MyClass[] Array;
public MyClass()
{
a = 5;
b = 2;
Array = new MyClass[3];
}

public void SetArrayFields()
{
Array[0].a = 1;
Array[0].b = 8;
}
}

What happens is that whenever I enter SetArrayFields() and try to set values
to the array there is an error "System.NullReferenceException"

Please help me to solve this problem
Many thanks

If you look at this with the debugger you will see that all elements
of the array are null.

The line:
Array = new MyClass[3];
allocates an array with all members set to null. You need to create
new instances of your class and store them in the array.

Besides that, your whole structure seems weird to me. Each instance
of your class contains an array of three instances of your class. Does
each of those instances also include 3 more instances? If so, your
class will occupy an infinite amount of memory.
 
When you create an Array, you are only allocating space for its members. You
have to set each Array member to an instance of the type it contains before
attempting to work with the members of that instance in the Array.

--
HTH,

Kevin Spencer
Microsoft MVP

DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

gol said:
Thanks, Jack
What do you suggest, then?
Should I make another class with an array of type MyClass and a public
method of inserting another element to the array.
And in the class MyClass - in the constructor, also to call that method
with
the element that has just been created.
How does it sound?

Or maybe it's better to use a list?
Thanks

Jack Jackson said:
Hi all,
I have an array defined in a class. The type of the array is the class
type.
For example:

class MyClass
{
private int a;
private int b;
private MyClass[] Array;
public MyClass()
{
a = 5;
b = 2;
Array = new MyClass[3];
}

public void SetArrayFields()
{
Array[0].a = 1;
Array[0].b = 8;
}
}

What happens is that whenever I enter SetArrayFields() and try to set
values
to the array there is an error "System.NullReferenceException"

Please help me to solve this problem
Many thanks

If you look at this with the debugger you will see that all elements
of the array are null.

The line:
Array = new MyClass[3];
allocates an array with all members set to null. You need to create
new instances of your class and store them in the array.

Besides that, your whole structure seems weird to me. Each instance
of your class contains an array of three instances of your class. Does
each of those instances also include 3 more instances? If so, your
class will occupy an infinite amount of memory.
 
Thank you Kevin,
Then, what do you think about my last idea?
is it good?
Thanks

Kevin Spencer said:
When you create an Array, you are only allocating space for its members. You
have to set each Array member to an instance of the type it contains before
attempting to work with the members of that instance in the Array.

--
HTH,

Kevin Spencer
Microsoft MVP

DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

gol said:
Thanks, Jack
What do you suggest, then?
Should I make another class with an array of type MyClass and a public
method of inserting another element to the array.
And in the class MyClass - in the constructor, also to call that method
with
the element that has just been created.
How does it sound?

Or maybe it's better to use a list?
Thanks

Jack Jackson said:
On Thu, 20 Sep 2007 08:30:09 -0700, gol

Hi all,
I have an array defined in a class. The type of the array is the class
type.
For example:

class MyClass
{
private int a;
private int b;
private MyClass[] Array;
public MyClass()
{
a = 5;
b = 2;
Array = new MyClass[3];
}

public void SetArrayFields()
{
Array[0].a = 1;
Array[0].b = 8;
}
}

What happens is that whenever I enter SetArrayFields() and try to set
values
to the array there is an error "System.NullReferenceException"

Please help me to solve this problem
Many thanks

If you look at this with the debugger you will see that all elements
of the array are null.

The line:
Array = new MyClass[3];
allocates an array with all members set to null. You need to create
new instances of your class and store them in the array.

Besides that, your whole structure seems weird to me. Each instance
of your class contains an array of three instances of your class. Does
each of those instances also include 3 more instances? If so, your
class will occupy an infinite amount of memory.
 
Without knowing more about what you are trying to accomplish I can't
tell.

Thanks, Jack
What do you suggest, then?
Should I make another class with an array of type MyClass and a public
method of inserting another element to the array.
And in the class MyClass - in the constructor, also to call that method with
the element that has just been created.
How does it sound?

Or maybe it's better to use a list?
Thanks

Jack Jackson said:
Hi all,
I have an array defined in a class. The type of the array is the class type.
For example:

class MyClass
{
private int a;
private int b;
private MyClass[] Array;
public MyClass()
{
a = 5;
b = 2;
Array = new MyClass[3];
}

public void SetArrayFields()
{
Array[0].a = 1;
Array[0].b = 8;
}
}

What happens is that whenever I enter SetArrayFields() and try to set values
to the array there is an error "System.NullReferenceException"

Please help me to solve this problem
Many thanks

If you look at this with the debugger you will see that all elements
of the array are null.

The line:
Array = new MyClass[3];
allocates an array with all members set to null. You need to create
new instances of your class and store them in the array.

Besides that, your whole structure seems weird to me. Each instance
of your class contains an array of three instances of your class. Does
each of those instances also include 3 more instances? If so, your
class will occupy an infinite amount of memory.
 
Hi,
OK, after working on this for a while I've come to this:
I have a simple class, something like this:
class MyClass
{
private int a;
private int b;
public MyClass()
{
a = 5;
b = 2;
}

public MyClass(int first, int second)
{
a = first;
b = second;
}

public int GetOneField(int Fieldindex)
{
switch (Fieldindex)
{
case 1:
return a;
break;
case 2:
return b;
break;
default:
return 0;
break;
}
}
}

And another class which contains an array of type MyClass, like this:

class MyClassRecords
{
private MyClass[] RecordsArray;
private MyClass OneRecord;
private static int index = 0;
public MyClassRecords()
{
OneRecord = new MyClass();
RecordsArray = new MyClass[3];
}

public MyClass[] GetRecordsArray()
{
return RecordsArray;
}

public void Insert()
{
RecordsArray[index] = OneRecord;
index++;
}

public void RecordSet(int a, int b)
{
OneRecord = new MyClass(a, b);
}
}

What I want is to insert data to the records array at the beginning, let's
say, in Form1, and after that use that data in some of the other forms of the
application.

In Form1 I have an instance of MyClassRecords like this:
private MyClassRecords Records = new MyClassRecords();

Then I insert data with parameters a,b which I received during the
application like this:
Records.RecordSet(a,b);
Records.Insert();

In other forms I want to use these data this way:
MyClassRecords Records = new MyClassRecords();
And then : if(Records.GetRecordsArray()[0].GetOneField(1) == 1)
{
//do something
}

I don't want to change the data in the other forms, only read and use the
data that has been inserted in Form1.

Now, of course this doesn't work, because here I have a new instance, but I
want to read the data that I've inserted in Form1.
In other words, the instance of MyClassRecords which I had in Form1 is no
longer "alive".

The question is, then, how to transfer information between forms?

In my case, I thought of making the class MyClassRecords as a singleton
class (a technique which I've learnt only few days ago) and this way its
array will always remain the same.
I don't know whether it's good to do it this way here.

Someone also told me I can pass the instance of MyClassRecords as a
parameter to the constructor of another form. Can I do it? And if so, is it
worth it? (considering I have to carry this instance with me through all the
forms of the application).

Sorry for the very long explanation,
Thanks a lot for your answer
 
Hi,
OK, after working on this for a while I've come to this:
I have a simple class, something like this:
class MyClass
{
private int a;
private int b;
public MyClass()
{
a = 5;
b = 2;
}

public MyClass(int first, int second)
{
a = first;
b = second;
}

public int GetOneField(int Fieldindex)
{
switch (Fieldindex)
{
case 1:
return a;
break;
case 2:
return b;
break;
default:
return 0;
break;
}
}
}

And another class which contains an array of type MyClass, like this:

class MyClassRecords
{
private MyClass[] RecordsArray;
private MyClass OneRecord;
private static int index = 0;
public MyClassRecords()
{
OneRecord = new MyClass();
RecordsArray = new MyClass[3];
}

public MyClass[] GetRecordsArray()
{
return RecordsArray;
}

public void Insert()
{
RecordsArray[index] = OneRecord;
index++;
}

public void RecordSet(int a, int b)
{
OneRecord = new MyClass(a, b);
}
}

What I want is to insert data to the records array at the beginning, let's
say, in Form1, and after that use that data in some of the other forms of the
application.

In Form1 I have an instance of MyClassRecords like this:
private MyClassRecords Records = new MyClassRecords();

Then I insert data with parameters a,b which I received during the
application like this:
Records.RecordSet(a,b);
Records.Insert();

In other forms I want to use these data this way:
MyClassRecords Records = new MyClassRecords();
And then : if(Records.GetRecordsArray()[0].GetOneField(1) == 1)
{
//do something
}

I don't want to change the data in the other forms, only read and use the
data that has been inserted in Form1.

Now, of course this doesn't work, because here I have a new instance, but I
want to read the data that I've inserted in Form1.
In other words, the instance of MyClassRecords which I had in Form1 is no
longer "alive".

The question is, then, how to transfer information between forms?

In my case, I thought of making the class MyClassRecords as a singleton
class (a technique which I've learnt only few days ago) and this way its
array will always remain the same.
I don't know whether it's good to do it this way here.

Someone also told me I can pass the instance of MyClassRecords as a
parameter to the constructor of another form. Can I do it? And if so, is it
worth it? (considering I have to carry this instance with me through all the
forms of the application).

Sorry for the very long explanation,
Thanks a lot for your answer

There are a couple of problems with your code.

First, you create one instance of MyClass which you put in
MyClassRecords.OneClass. Then in Insert() you assign that one
instance to the next entry in the array. That means that each entry
in the array points to the same instance of the class. You need to
create a new instance each time.

Why is SetRecord separate from Insert? Don't you always want to call
both? If so, get rid of Insert and change SetRecord to:

public void RecordSet(int a, int b)
{
RecordsArray[index] = New MyClass(a, b);
index++;
}

Do you really want the class hard coded to have at most three
instances of the class? You at least need a check to make sure you
don't call Insert more than 3 times. I would probably use and
ArrayList or the generic List instead of the array.

I would pass the reference to the instance of MyClassRecords to the
other forms. You can to it either as a constructor parameter or by
calling a method in the other forms.
 
Thank you very much for your answer, Jack
I took your advice of uniting Insert and RecordSet, and also removed the
instance of MyClass and creating a new instance each time.

About the hard coded size of the array – I know about it, it is only
temporary, and I know that I have to do something about it.

I understand now that I can pass the class as a parameter to the
constructor, or to a method.
Can you, please, tell me why is it not so good to leave it the way I did
(make the class as a singleton class)?
Thank you very much


Jack Jackson said:
Hi,
OK, after working on this for a while I've come to this:
I have a simple class, something like this:
class MyClass
{
private int a;
private int b;
public MyClass()
{
a = 5;
b = 2;
}

public MyClass(int first, int second)
{
a = first;
b = second;
}

public int GetOneField(int Fieldindex)
{
switch (Fieldindex)
{
case 1:
return a;
break;
case 2:
return b;
break;
default:
return 0;
break;
}
}
}

And another class which contains an array of type MyClass, like this:

class MyClassRecords
{
private MyClass[] RecordsArray;
private MyClass OneRecord;
private static int index = 0;
public MyClassRecords()
{
OneRecord = new MyClass();
RecordsArray = new MyClass[3];
}

public MyClass[] GetRecordsArray()
{
return RecordsArray;
}

public void Insert()
{
RecordsArray[index] = OneRecord;
index++;
}

public void RecordSet(int a, int b)
{
OneRecord = new MyClass(a, b);
}
}

What I want is to insert data to the records array at the beginning, let's
say, in Form1, and after that use that data in some of the other forms of the
application.

In Form1 I have an instance of MyClassRecords like this:
private MyClassRecords Records = new MyClassRecords();

Then I insert data with parameters a,b which I received during the
application like this:
Records.RecordSet(a,b);
Records.Insert();

In other forms I want to use these data this way:
MyClassRecords Records = new MyClassRecords();
And then : if(Records.GetRecordsArray()[0].GetOneField(1) == 1)
{
//do something
}

I don't want to change the data in the other forms, only read and use the
data that has been inserted in Form1.

Now, of course this doesn't work, because here I have a new instance, but I
want to read the data that I've inserted in Form1.
In other words, the instance of MyClassRecords which I had in Form1 is no
longer "alive".

The question is, then, how to transfer information between forms?

In my case, I thought of making the class MyClassRecords as a singleton
class (a technique which I've learnt only few days ago) and this way its
array will always remain the same.
I don't know whether it's good to do it this way here.

Someone also told me I can pass the instance of MyClassRecords as a
parameter to the constructor of another form. Can I do it? And if so, is it
worth it? (considering I have to carry this instance with me through all the
forms of the application).

Sorry for the very long explanation,
Thanks a lot for your answer

There are a couple of problems with your code.

First, you create one instance of MyClass which you put in
MyClassRecords.OneClass. Then in Insert() you assign that one
instance to the next entry in the array. That means that each entry
in the array points to the same instance of the class. You need to
create a new instance each time.

Why is SetRecord separate from Insert? Don't you always want to call
both? If so, get rid of Insert and change SetRecord to:

public void RecordSet(int a, int b)
{
RecordsArray[index] = New MyClass(a, b);
index++;
}

Do you really want the class hard coded to have at most three
instances of the class? You at least need a check to make sure you
don't call Insert more than 3 times. I would probably use and
ArrayList or the generic List instead of the array.

I would pass the reference to the instance of MyClassRecords to the
other forms. You can to it either as a constructor parameter or by
calling a method in the other forms.
 
The singleton is OK too. A slight advantage of passing the reference
is that if you ever need another instance of the class you could
easily do it. Perhaps some day not all forms would share the same
instance.

Thank you very much for your answer, Jack
I took your advice of uniting Insert and RecordSet, and also removed the
instance of MyClass and creating a new instance each time.

About the hard coded size of the array – I know about it, it is only
temporary, and I know that I have to do something about it.

I understand now that I can pass the class as a parameter to the
constructor, or to a method.
Can you, please, tell me why is it not so good to leave it the way I did
(make the class as a singleton class)?
Thank you very much


Jack Jackson said:
Hi,
OK, after working on this for a while I've come to this:
I have a simple class, something like this:
class MyClass
{
private int a;
private int b;
public MyClass()
{
a = 5;
b = 2;
}

public MyClass(int first, int second)
{
a = first;
b = second;
}

public int GetOneField(int Fieldindex)
{
switch (Fieldindex)
{
case 1:
return a;
break;
case 2:
return b;
break;
default:
return 0;
break;
}
}
}

And another class which contains an array of type MyClass, like this:

class MyClassRecords
{
private MyClass[] RecordsArray;
private MyClass OneRecord;
private static int index = 0;
public MyClassRecords()
{
OneRecord = new MyClass();
RecordsArray = new MyClass[3];
}

public MyClass[] GetRecordsArray()
{
return RecordsArray;
}

public void Insert()
{
RecordsArray[index] = OneRecord;
index++;
}

public void RecordSet(int a, int b)
{
OneRecord = new MyClass(a, b);
}
}

What I want is to insert data to the records array at the beginning, let's
say, in Form1, and after that use that data in some of the other forms of the
application.

In Form1 I have an instance of MyClassRecords like this:
private MyClassRecords Records = new MyClassRecords();

Then I insert data with parameters a,b which I received during the
application like this:
Records.RecordSet(a,b);
Records.Insert();

In other forms I want to use these data this way:
MyClassRecords Records = new MyClassRecords();
And then : if(Records.GetRecordsArray()[0].GetOneField(1) == 1)
{
//do something
}

I don't want to change the data in the other forms, only read and use the
data that has been inserted in Form1.

Now, of course this doesn't work, because here I have a new instance, but I
want to read the data that I've inserted in Form1.
In other words, the instance of MyClassRecords which I had in Form1 is no
longer "alive".

The question is, then, how to transfer information between forms?

In my case, I thought of making the class MyClassRecords as a singleton
class (a technique which I've learnt only few days ago) and this way its
array will always remain the same.
I don't know whether it's good to do it this way here.

Someone also told me I can pass the instance of MyClassRecords as a
parameter to the constructor of another form. Can I do it? And if so, is it
worth it? (considering I have to carry this instance with me through all the
forms of the application).

Sorry for the very long explanation,
Thanks a lot for your answer

There are a couple of problems with your code.

First, you create one instance of MyClass which you put in
MyClassRecords.OneClass. Then in Insert() you assign that one
instance to the next entry in the array. That means that each entry
in the array points to the same instance of the class. You need to
create a new instance each time.

Why is SetRecord separate from Insert? Don't you always want to call
both? If so, get rid of Insert and change SetRecord to:

public void RecordSet(int a, int b)
{
RecordsArray[index] = New MyClass(a, b);
index++;
}

Do you really want the class hard coded to have at most three
instances of the class? You at least need a check to make sure you
don't call Insert more than 3 times. I would probably use and
ArrayList or the generic List instead of the array.

I would pass the reference to the instance of MyClassRecords to the
other forms. You can to it either as a constructor parameter or by
calling a method in the other forms.
 
Yes, ok, I get your point.
Thanks a lot for your help.
I appreciate it very much

Jack Jackson said:
The singleton is OK too. A slight advantage of passing the reference
is that if you ever need another instance of the class you could
easily do it. Perhaps some day not all forms would share the same
instance.

Thank you very much for your answer, Jack
I took your advice of uniting Insert and RecordSet, and also removed the
instance of MyClass and creating a new instance each time.

About the hard coded size of the array – I know about it, it is only
temporary, and I know that I have to do something about it.

I understand now that I can pass the class as a parameter to the
constructor, or to a method.
Can you, please, tell me why is it not so good to leave it the way I did
(make the class as a singleton class)?
Thank you very much


Jack Jackson said:
On Mon, 24 Sep 2007 01:30:00 -0700, gol

Hi,
OK, after working on this for a while I've come to this:
I have a simple class, something like this:
class MyClass
{
private int a;
private int b;
public MyClass()
{
a = 5;
b = 2;
}

public MyClass(int first, int second)
{
a = first;
b = second;
}

public int GetOneField(int Fieldindex)
{
switch (Fieldindex)
{
case 1:
return a;
break;
case 2:
return b;
break;
default:
return 0;
break;
}
}
}

And another class which contains an array of type MyClass, like this:

class MyClassRecords
{
private MyClass[] RecordsArray;
private MyClass OneRecord;
private static int index = 0;
public MyClassRecords()
{
OneRecord = new MyClass();
RecordsArray = new MyClass[3];
}

public MyClass[] GetRecordsArray()
{
return RecordsArray;
}

public void Insert()
{
RecordsArray[index] = OneRecord;
index++;
}

public void RecordSet(int a, int b)
{
OneRecord = new MyClass(a, b);
}
}

What I want is to insert data to the records array at the beginning, let's
say, in Form1, and after that use that data in some of the other forms of the
application.

In Form1 I have an instance of MyClassRecords like this:
private MyClassRecords Records = new MyClassRecords();

Then I insert data with parameters a,b which I received during the
application like this:
Records.RecordSet(a,b);
Records.Insert();

In other forms I want to use these data this way:
MyClassRecords Records = new MyClassRecords();
And then : if(Records.GetRecordsArray()[0].GetOneField(1) == 1)
{
//do something
}

I don't want to change the data in the other forms, only read and use the
data that has been inserted in Form1.

Now, of course this doesn't work, because here I have a new instance, but I
want to read the data that I've inserted in Form1.
In other words, the instance of MyClassRecords which I had in Form1 is no
longer "alive".

The question is, then, how to transfer information between forms?

In my case, I thought of making the class MyClassRecords as a singleton
class (a technique which I've learnt only few days ago) and this way its
array will always remain the same.
I don't know whether it's good to do it this way here.

Someone also told me I can pass the instance of MyClassRecords as a
parameter to the constructor of another form. Can I do it? And if so, is it
worth it? (considering I have to carry this instance with me through all the
forms of the application).

Sorry for the very long explanation,
Thanks a lot for your answer

There are a couple of problems with your code.

First, you create one instance of MyClass which you put in
MyClassRecords.OneClass. Then in Insert() you assign that one
instance to the next entry in the array. That means that each entry
in the array points to the same instance of the class. You need to
create a new instance each time.

Why is SetRecord separate from Insert? Don't you always want to call
both? If so, get rid of Insert and change SetRecord to:

public void RecordSet(int a, int b)
{
RecordsArray[index] = New MyClass(a, b);
index++;
}

Do you really want the class hard coded to have at most three
instances of the class? You at least need a check to make sure you
don't call Insert more than 3 times. I would probably use and
ArrayList or the generic List instead of the array.

I would pass the reference to the instance of MyClassRecords to the
other forms. You can to it either as a constructor parameter or by
calling a method in the other forms.
 
Back
Top