What am I doing wrong?

M

mb

I have two .cs files and I am trying to have them interact in the project.

One is Form1.cs and the other is Define.cs

Form1 has code that creates a type "Cars"

I instantiate an array of type Test with properties

public static Cars[] myCar = new Cars[17];

public class Cars
{
public string Wheels;
...
}

Now, I have 17 cars that have specific properties like one has Cars.Wheels =
Chrome, etc.
However, I don't want to waste the space in my Form1.cs file, AND I may want
to use the 17 car's info in other programs. Therefore, I decided to make a
separate .cs file called Define to simply have the info for these cars.
However, when I try to call the Define file's constructor, it stops on the
first assignment line:

Cars[0].Wheels = "Chrome";

and says "Object reference not set to an instance of an object"

why is this. I feel that i have tried alll I can. I think I am missing
some understanding here. Could someone please help me?
 
H

Hermit Dave

when you use

Classtype[] myobjects = new Classtype[number];
it creates an array with references set to null
you will need to use

myobjects[1] = new Classtype(constructor)

if the type is a value type then the array is initialized but in this case
its a reference type and you will need to initialize it

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
mb said:
You know, that problem seems to occur when I instantiate an array of type
Cars.

mb said:
I have two .cs files and I am trying to have them interact in the project.

One is Form1.cs and the other is Define.cs

Form1 has code that creates a type "Cars"

I instantiate an array of type Test with properties

public static Cars[] myCar = new Cars[17];

public class Cars
{
public string Wheels;
...
}

Now, I have 17 cars that have specific properties like one has
Cars.Wheels
=
Chrome, etc.
However, I don't want to waste the space in my Form1.cs file, AND I may want
to use the 17 car's info in other programs. Therefore, I decided to
make
a
separate .cs file called Define to simply have the info for these cars.
However, when I try to call the Define file's constructor, it stops on the
first assignment line:

Cars[0].Wheels = "Chrome";

and says "Object reference not set to an instance of an object"

why is this. I feel that i have tried alll I can. I think I am missing
some understanding here. Could someone please help me?
 
M

mb

so, instead of:
Classtype[] myobjects = new Classtype[number];

I need to use:

myobjects[0] = new Classtype(constructor);
myobjects[1] = new Classtype(constructor);
....

Is that right? Or do I use both?

Also, is it a reference because it is a type from another .cs file?




Hermit Dave said:
when you use

Classtype[] myobjects = new Classtype[number];
it creates an array with references set to null
you will need to use

myobjects[1] = new Classtype(constructor)

if the type is a value type then the array is initialized but in this case
its a reference type and you will need to initialize it

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
mb said:
You know, that problem seems to occur when I instantiate an array of type
Cars.

mb said:
I have two .cs files and I am trying to have them interact in the project.

One is Form1.cs and the other is Define.cs

Form1 has code that creates a type "Cars"

I instantiate an array of type Test with properties

public static Cars[] myCar = new Cars[17];

public class Cars
{
public string Wheels;
...
}

Now, I have 17 cars that have specific properties like one has
Cars.Wheels
=
Chrome, etc.
However, I don't want to waste the space in my Form1.cs file, AND I
may
want
to use the 17 car's info in other programs. Therefore, I decided to
make
a
separate .cs file called Define to simply have the info for these cars.
However, when I try to call the Define file's constructor, it stops on the
first assignment line:

Cars[0].Wheels = "Chrome";

and says "Object reference not set to an instance of an object"

why is this. I feel that i have tried alll I can. I think I am missing
some understanding here. Could someone please help me?
 
D

Daniel O'Connell [C# MVP]

mb said:
so, instead of:
Classtype[] myobjects = new Classtype[number];

I need to use:

myobjects[0] = new Classtype(constructor);
myobjects[1] = new Classtype(constructor);
...

Is that right? Or do I use both?

Both.

new Classtype[number] creates the array
new Classtype(constructor) creates the individual objects which you then
store in the array.
Also, is it a reference because it is a type from another .cs file?
what do you mean by that?
 
M

mb

I original post mentions that I want to store all of the 17 cars properties
in a seperate .cs file so I could reuse it. The first .cs file has the Cars
class, then I initiallize all the cars in another .cs file. Maybe this
doesn't make a difference.



Daniel O'Connell said:
mb said:
so, instead of:
Classtype[] myobjects = new Classtype[number];

I need to use:

myobjects[0] = new Classtype(constructor);
myobjects[1] = new Classtype(constructor);
...

Is that right? Or do I use both?

Both.

new Classtype[number] creates the array
new Classtype(constructor) creates the individual objects which you then
store in the array.
Also, is it a reference because it is a type from another .cs file?
what do you mean by that?
 
H

Hermit Dave

what you need to do is this

int arraylength = 17;
ClassType[] myCT = new ClassType[arraylength]; // this initializes the array
with null references

myCT[0] = new ClassType(the parameters list for contructor); // this creates
a new instance inplace of the null reference earlier.

// you want to add second car
myCT[1] = new ClassType(second set of params)

-------------------------------------------
that is one way the other way would be to create you own Custom collection
class which has an array as internal member.
Create your add method which will add the newly created car object to the
collection. With arraylist you do not have to worry about the length and you
create the object when you need it and add it to the colleciton

hope this help

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
mb said:
I original post mentions that I want to store all of the 17 cars properties
in a seperate .cs file so I could reuse it. The first .cs file has the Cars
class, then I initiallize all the cars in another .cs file. Maybe this
doesn't make a difference.



Daniel O'Connell said:
mb said:
so, instead of:
Classtype[] myobjects = new Classtype[number];

I need to use:

myobjects[0] = new Classtype(constructor);
myobjects[1] = new Classtype(constructor);
...

Is that right? Or do I use both?

Both.

new Classtype[number] creates the array
new Classtype(constructor) creates the individual objects which you then
store in the array.
Also, is it a reference because it is a type from another .cs file?
what do you mean by that?
 

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