Home All Groups Group Topic Archive Search About

unbounded array of objects gets error when instantiating elements

Author
31 Mar 2005 1:50 PM
DanielNoack
Hi,
I have a problem that I am unable to get an array of objects to
instantiate properly when it is declared with no upper bound. My code
is as follows


dim mParams() as clsParameters
mParams(0) = new clsParameters

produces an error saying mParams is set to nothing however if I change
the declaration to have an upper bound it works like


dim mParams(3) as clsParameters
mParams(0) = new clsParameters


this works but I need the array to be unbounded, does anyone know what
I am doing wrong or if this is possible to have an unbounded array of
objects in VB.NET ??

Thanks for any help.
Daniel

Author
31 Mar 2005 2:00 PM
Cor Ligthert
Daniel,

The most simple one for your problem is mos probably the arraylist

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionsarraylistclasstopic.asp

I hope this helps,

Cor
Author
31 Mar 2005 2:02 PM
Chris Dunaway
You can either use an ArrayList which is unbounded or you can use the
ReDim Preserve method:

Dim mParams() as clsParameters
ReDim mParams(2)
Author
31 Mar 2005 2:09 PM
Herfried K. Wagner [MVP]
"DanielNoack" <danielno***@hotmail.com> schrieb:
> I have a problem that I am unable to get an array of objects to
> instantiate properly when it is declared with no upper bound. My code
> is as follows
>
>
> dim mParams() as clsParameters
> mParams(0) = new clsParameters
>
> produces an error saying mParams is set to nothing however if I change
> the declaration to have an upper bound it works like

'mParams' doesn't contain any items, thus no item with index 0 exists.

> dim mParams(3) as clsParameters
> mParams(0) = new clsParameters

'mParams' contains four items with indices 0 to 3.

> this works but I need the array to be unbounded, does anyone know what
> I am doing wrong or if this is possible to have an unbounded array of
> objects in VB.NET ??

\\\
Dim Parameters() As Parameter = {New Parameter(), New Parameter()}
///

You can change an array's size later using 'ReDim Preserve'.  Notice that
'ReDim Preserve' will create a new array of the desired size and copy the
contents of the old array to the new array, which is a costly process,
especially for larger arrays.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>