473,587 Members | 2,580 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 1540
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******** ******@TK2MSFTN GP09.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(const ructor)

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******** ******@TK2MSFTN GP15.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******** ******@TK2MSFTN GP09.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(const ructor);
myobjects[1] = new Classtype(const ructor);
....

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******** ********@tk2msf tngp13.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(const ructor)

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******** ******@TK2MSFTN GP15.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******** ******@TK2MSFTN GP09.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******** ******@TK2MSFTN GP10.phx.gbl...
so, instead of:
Classtype[] myobjects = new Classtype[number];

I need to use:

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

Is that right? Or do I use both?
Both.

new Classtype[number] creates the array
new Classtype(const ructor) 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$******** ******@TK2MSFTN GP11.phx.gbl...

"mb" <mm@hotmail.com > wrote in message
news:uW******** ******@TK2MSFTN GP10.phx.gbl...
so, instead of:
Classtype[] myobjects = new Classtype[number];

I need to use:

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

Is that right? Or do I use both?


Both.

new Classtype[number] creates the array
new Classtype(const ructor) 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(secon d 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******** ******@TK2MSFTN GP15.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$******** ******@TK2MSFTN GP11.phx.gbl...

"mb" <mm@hotmail.com > wrote in message
news:uW******** ******@TK2MSFTN GP10.phx.gbl...
so, instead of:
Classtype[] myobjects = new Classtype[number];

I need to use:

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

Is that right? Or do I use both?


Both.

new Classtype[number] creates the array
new Classtype(const ructor) 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
3361
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 additional combo control doing a different filtering on the same form, and simply copied the code below and changed the appropriate combo names....
2
2701
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 button, seems simple. I am using ALL visual controls (supposedly to simplify things. If I was not using the visual controls and calling an ExecuteNonQuery...
2
1795
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 vb file containing: Imports System.Data.SqlClient Imports System.Web.HttpUtility
2
1184
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 = String.Concat("D_STAT_CD_C <> '", "N'")
0
2657
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 AccountDebitID and AccountCreditID does not work. 'Delete a transaction Sub deletetrans(ByVal transactionid As Integer, ByVal transactionamount
8
2060
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 vertices and can do this multiple times on a sinlge vertex cluster. The problem I have been encoutering is that, if I select a second vertex...
0
1319
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 child controls (just a literal for testing) of my control is not being added to the page. Could someone tell me what am I doing wrong?
16
1902
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 simpler answer, by all means show me the light! What I'm trying to do is refresh the page at a timed interval ( actually redirect the page... )...
10
1919
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
1788
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
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5713
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2353
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 we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.