Using array without knowing its size

  • Thread starter Thread starter Morten Snedker
  • Start date Start date
M

Morten Snedker

When using an array - and you initially don't know many "records" it
is to hold - how is the proper way of dimensioning it?

In my previous thread I'm looping folders. Let's say these are to be
put into an array. Initially I don't know the number of folders being
returned, so how would i dimension the array properly?

I've learned that ReDim is somewhat ressource-eating, but maybe that
is the way to go in the given scenario?


Regards /Snedker
 
If, as with my reply above, you use an ArrayList, then the array will
dynamically resize itself. All you do is "Add" items to it. When you are
done, you can look at "Count" property to see how many items you have
collected. ArrayLists are waaaay more efficient in this scenario than any
kind of "ReDim".
 
Morten,

Don't try to use a fixed array when you don't know for sure that it is
always fixed or have absolutly to use it.

There are so many arrays and collections in VBNet and you can make them even
yourself.

In my opinion is the most simplest one (the first to choose) is the one as
Robin told as well the ArrayList.

Cor
 
If you still need an array, you can always call ReDim once the ArrayList is
filled and copy the records over. Since you are just copying pointers, it
shouldn't be costly.
 

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