473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

What To Use Instead of "New" Keyword When Creating Class Array

Hi:

I'm having trouble using an Object which is created based on the following:

Public CarDetail () as Car
Where the CLASS "Car" is defined as:

Public Class Car
Public Doors() as string
Public Color as string
End Class

While it appears that I can create the "CarDetail" Object, and exception is
thrown when I try to refer to anything that is part of "CarDetail".

I am receiving the following error: "Object reference not set to an instance
of an object.".

Ok, so I figure I need to add the "New" Keyword when I instantiate
"CarDetail":

Public CarDetail () as Car -----> Public CarDetail () as New Car

When I do this, the compilier informs me that 'arrays cannot be declared
with 'New'.

Is there an alternative to the "New" keyword that I should use in this case?

Thanks
Paul Auleciems

Nov 22 '05 #1
6 1941
Paul Auleciems <au**********@newsgroup.nospam> wrote:
I'm having trouble using an Object which is created based on the following:

Public CarDetail () as Car

Where the CLASS "Car" is defined as:

Public Class Car
Public Doors() as string
Public Color as string
End Class

While it appears that I can create the "CarDetail" Object, and exception is
thrown when I try to refer to anything that is part of "CarDetail".

I am receiving the following error: "Object reference not set to an instance
of an object.".

Ok, so I figure I need to add the "New" Keyword when I instantiate
"CarDetail":

Public CarDetail () as Car -----> Public CarDetail () as New Car

When I do this, the compilier informs me that 'arrays cannot be declared
with 'New'.

Is there an alternative to the "New" keyword that I should use in this case?


After creating the array, each element of it is null (Nothing). You
need to set the element's value to a new instance of CarDetail before
accessing it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #2
Paul Auleciems <au**********@newsgroup.nospam> wrote:
I'm having trouble using an Object which is created based on the following:

Public CarDetail () as Car

Where the CLASS "Car" is defined as:

Public Class Car
Public Doors() as string
Public Color as string
End Class

While it appears that I can create the "CarDetail" Object, and exception is
thrown when I try to refer to anything that is part of "CarDetail".

I am receiving the following error: "Object reference not set to an instance
of an object.".

Ok, so I figure I need to add the "New" Keyword when I instantiate
"CarDetail":

Public CarDetail () as Car -----> Public CarDetail () as New Car

When I do this, the compilier informs me that 'arrays cannot be declared
with 'New'.

Is there an alternative to the "New" keyword that I should use in this case?


After creating the array, each element of it is null (Nothing). You
need to set the element's value to a new instance of CarDetail before
accessing it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #3
Is there an alternative to the "New" keyword that I should use in this case?


Public CarDetail(N) as Car

to create an array with N+1 elements.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 22 '05 #4
Is there an alternative to the "New" keyword that I should use in this case?


Public CarDetail(N) as Car

to create an array with N+1 elements.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 22 '05 #5
Hi Paul,

You have to create each Car object in the array like the following:

For i=0 to N
CarDetail(i) = New Car()
Next i

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 22 '05 #6
Hi Paul,

You have to create each Car object in the array like the following:

For i=0 to N
CarDetail(i) = New Car()
Next i

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 22 '05 #7

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

Similar topics

3
by: Paul Auleciems | last post by:
Hi: I'm having trouble using an Object which is created based on the following: Public CarDetail () as Car Where the CLASS "Car" is defined as: Public Class Car
1
by: alanrn | last post by:
I've implemented a number of strongly-typed collections that inherit from CollectionBase and recently noticed something that I don't fully understand. CollectionBase defines method RemoveAt(). ...
6
by: dpr | last post by:
I have come accross a piece of C++ code with the construct: MyClass *c = new class MyClass(); Is there a difference between this and: MyClass *c = new MyClass(); ?
30
by: seesaw | last post by:
Is it right thing to always avoid using "new" to create objects? What if after starting the application, then decide which and how many objects to create? (Seems like under such situation is there...
6
by: Jason Shohet | last post by:
2 questions for anyone who can answer: 1. in class declarations, I realize you have to use the NEW keyword if you want to declare & instantiate some class at the same time. Whats an advantage...
3
by: Danny Din | last post by:
Hi, I have the following code – public class Class1 { public Class1()
12
by: Ola Johansson | last post by:
Can anyone explain to me a meningfull use of the "new" keyword (Shadows in VB). I think i understand how it works fully but i cant figure out why you would use it. Why would you want to declare...
8
by: Dot net work | last post by:
I need VB.NET's "shadows" functionality inside a C# project. I tried the "new" keyword, but it didn't seem to work, because my particular function does in fact differ in signature to the function...
5
by: Sam | last post by:
Hi everyone Could anyone help me understand the usage of the "New" keyword I'm new to VB.Net. 1. Why do we use the "New" keyword on some object type variables such as the myPen of the...
4
by: Ben R. | last post by:
I'm curious about the differeng behavior of the "new" keyword when dealing with value versus object types. If I'm correct, when I do: dim x as integer There's no need for "new" to be called...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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...
0
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...
0
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...

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.