best way to Dim arrays you don't know the size of?

  • Thread starter Thread starter Scott M
  • Start date Start date
S

Scott M

If you don't know the size of an array when you create it (and it
needs to be available to the whole program), is it more efficient to
redim or use (,) e.g. :

1.

Public class form1
….
Dim SampleArray(,) as string


Private sub Something()
Dim NowWeKnowTheValueOfTheArray as integer = 100
Dim NowWeKnowTheValueOfTheArray2 as integer = 1000

For i = 0 to NowWeKnowTheValueOfTheArray - 1
For j = 0 to NowWeKnowTheValueOfTheArray2 - 1
SampleArray(i, j) = "Something"
next
Next

Or

2.

Public class form1
…..
Dim SampleArray(0,1) as string
…..

Private sub Something()
Dim NowWeKnowTheValueOfTheArray as integer = 100
Dim NowWeKnowTheValueOfTheArray2 as integer = 1000

Redim SampleArray(NowWeKnowTheValueOfTheArray - 1 ,
NowWeKnowTheValueOfTheArray2 - 1)

For i = 0 to NowWeKnowTheValueOfTheArray - 1
For j = 0 to NowWeKnowTheValueOfTheArray2 - 1
SampleArray(i, j) = "Something"
next
Next

Or something else?
 
It is best to use an ArrayList object that can grow as you need it.

You can set an initial size.

--- Nick
 
Back
Top