473,473 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

array of objects

Hi,
I have trouble with an array of objects!

I declared a class like this:

Public Class myClass
Public X As String
Public Y As Integer
Public Z As String

Public Sub New()
X = ""
Y = 0
Z = ""
End Sub

End Class

when in another class i try to declare an instance of it

Dim MyArray as New myClass

myArray.X = "ggggg"

it's work fine, but if i try to declare an array of objects using

Dim MyArray() as myClass
myArray(0).X = "ggggg"

i get the following error message:

Unhandled exception "System.NullReferenceException" in myProject.exe

Reference to an object without object instance.

can you help me with it?

thanks

Michele
Nov 20 '05 #1
2 2020
When you declare an array, you do not filled it with instances yet, so you
need to do this first!
Dim MyArray(10) as myClass
myArray(0) = new myClass()
myArray(0).X = "Testing"

Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
"Michele" <mi****@tin.it> wrote in message
news:31*************************@posting.google.co m...
Hi,
I have trouble with an array of objects!

I declared a class like this:

Public Class myClass
Public X As String
Public Y As Integer
Public Z As String

Public Sub New()
X = ""
Y = 0
Z = ""
End Sub

End Class

when in another class i try to declare an instance of it

Dim MyArray as New myClass

myArray.X = "ggggg"

it's work fine, but if i try to declare an array of objects using

Dim MyArray() as myClass
myArray(0).X = "ggggg"

i get the following error message:

Unhandled exception "System.NullReferenceException" in myProject.exe

Reference to an object without object instance.

can you help me with it?

thanks

Michele

Nov 20 '05 #2
* mi****@tin.it (Michele) scripsit:
I have trouble with an array of objects!

I declared a class like this:

Public Class myClass
Public X As String
Public Y As Integer
Public Z As String

Public Sub New()
X = ""
Y = 0
Z = ""
End Sub

End Class

when in another class i try to declare an instance of it

Dim MyArray as New myClass

myArray.X = "ggggg"

it's work fine, but if i try to declare an array of objects using

Dim MyArray() as myClass
myArray(0).X = "ggggg"

i get the following error message:

Unhandled exception "System.NullReferenceException" in myProject.exe


The code will only create an array, but it won't instantiate the
objects. You can use 'MyArray(0) = New MyClass()' before accessing one
of its properties to do that. Notice that 'MyClass' is a reserved
keyword in VB.NET.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #3

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

Similar topics

4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
2
by: James | last post by:
Hi, I'm hoping someone can help me out. If I declare a class, eg. class CSomeclass { public: var/func etc..... private varfunc etc..
7
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
19
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
2
by: JJA | last post by:
I'm looking at some code I do not understand: var icons = new Array(); icons = new GIcon(); icons.image = "somefilename.png"; I read this as an array of icons is being built. An element of...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
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
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
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...
1
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...
1
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...
0
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...
0
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 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.