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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top