unbounded array of objects gets error when instantiating elements

  • Thread starter Thread starter DanielNoack
  • Start date Start date
D

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
 
You can either use an ArrayList which is unbounded or you can use the
ReDim Preserve method:

Dim mParams() as clsParameters
ReDim mParams(2)
 
DanielNoack said:
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.
 
Back
Top