473,511 Members | 14,799 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What am I doing wrong?

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?
Jul 21 '05 #1
7 1527
mb
You know, that problem seems to occur when I instantiate an array of type
Cars.

"mb" <mm@hotmail.com> wrote in message
news:OU**************@TK2MSFTNGP09.phx.gbl...
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?

Jul 21 '05 #2
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" <mm@hotmail.com> wrote in message
news:O3**************@TK2MSFTNGP15.phx.gbl...
You know, that problem seems to occur when I instantiate an array of type
Cars.

"mb" <mm@hotmail.com> wrote in message
news:OU**************@TK2MSFTNGP09.phx.gbl...
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?


Jul 21 '05 #3
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" <he************@CAPS.AND.DOTS.hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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" <mm@hotmail.com> wrote in message
news:O3**************@TK2MSFTNGP15.phx.gbl...
You know, that problem seems to occur when I instantiate an array of type
Cars.

"mb" <mm@hotmail.com> wrote in message
news:OU**************@TK2MSFTNGP09.phx.gbl...
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?



Jul 21 '05 #4

"mb" <mm@hotmail.com> wrote in message
news:uW**************@TK2MSFTNGP10.phx.gbl...
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?
Jul 21 '05 #5
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 [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:O$**************@TK2MSFTNGP11.phx.gbl...

"mb" <mm@hotmail.com> wrote in message
news:uW**************@TK2MSFTNGP10.phx.gbl...
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?

Jul 21 '05 #6
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" <mm@hotmail.com> wrote in message
news:u5**************@TK2MSFTNGP15.phx.gbl...
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 [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:O$**************@TK2MSFTNGP11.phx.gbl...

"mb" <mm@hotmail.com> wrote in message
news:uW**************@TK2MSFTNGP10.phx.gbl...
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?


Jul 21 '05 #7
On Sat, 18 Sep 2004 12:33:22 -0600, mb wrote:
Also, is it a reference because it is a type from another .cs file?


It's a reference type because it's declared as a class.

Explanation: http://www.yoda.arachsys.com/csharp/parameters.html
Jul 21 '05 #8

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

Similar topics

7
3354
by: Vic | last post by:
Dear All, I found this code snippet on this list (taken from a nice webpage of a courteous fellow), which I used to filter a form on a combo box. I wanted to repeat the same code to have an...
2
2697
by: Aaron Ackerman | last post by:
I cannot a row to this bound DataGrid to SAVE MY LIFE! I have tried everything and I am at a loss. The using goes into add mode with the add button adds his data then updates with the update...
2
1792
by: Phil Certain | last post by:
Hi, Relative newbie to .Net but experienced with classic ASP. I am trying to create a simple business object to contain commonly used functions. This is what I have done: 1 - Created a simple...
2
1179
by: vvenk | last post by:
Hello: I have the following code: Dim lsFilter As String = Nothing Select Case rblStatus.SelectedItem.Value Case "S" lsFilter = String.Concat("D_STAT_CD_C = '", "N'") Case "R" lsFilter =...
0
2652
by: Steve | last post by:
I have a gridview which uses an objectdatasource for its select and delete. The delete command uses the function below. The delete itself works but the extra logic which requires parameters...
8
2056
by: watkinsdev | last post by:
Hi, I have created a mesh class in visual studio 6.0 c++. I can create a device, render objects and can edit the objects by for instancnce selecting a cluster of vertices and processing the...
0
1316
by: shapper | last post by:
Hello, I am creating a class with a control. I compiled the class and used it on an Asp.Net 2.0 web site page. I can see the begin and end tags of my control (<oland </ol>) but somehow the...
16
1893
by: SirG | last post by:
I'm looking for an explanation of why one piece of code works and another does not. I have to warn you that this is the first piece of Javascript I've ever written, so if there is a better way or a...
10
1907
by: Enkidu | last post by:
Beginner question, sorry! I am using indexers to access an array of StringBuilders in an instance of a class: Getting: value = board1; Setting: board1 = value1 ;
10
1774
by: DavidSeck.com | last post by:
Hi, I am working with the Facebook API right now, an I have kind of a problem, but I don't know what I am doing wrong. So I have a few arrays, f.ex.: User albums: array(2) {
0
7252
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
7371
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
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5676
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,...
0
4743
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
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1583
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
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.