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? 7 1527
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?
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?
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?
"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?
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?
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?
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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 =...
|
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...
| |
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...
|
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...
|
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...
|
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 ;
|
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) {
|
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,...
| |
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,...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |