Creating an array with an unknown number of values?

D

darrel

I'm trying to create an array with a size I don't know.

For instance:

Dim values() As Integer
Dim valuesString As String = ""
while something
valuesString = valuesString & newValue 7 ", "
end while
....trim off last ", "...
values = new integer() {valuesString}

However, in attempting to put the valuesString into the array, I get this
error:

System.FormatException: Input string was not in a correct format.

So, it looks like I can't put a comma-delimited string in as the values for
an array. As such, how should I approach this?

I went ahead and made two WHILE loops...the first to simply count the number
of items I need, then dim the array with a specific size, then do another
while loop to populate each of the values. Is there a more elegant way?

-Darrel
 
K

Kevin Spencer

Why not use a List or Collection? That is what they are for.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 
D

darrel

Why not use a List or Collection? That is what they are for.

Well, I read an arrayList has a lot more overhead. But if that's what it's
for, then that's probably what I should use. ;o)

-Darrel
 
K

Kevin Spencer

Hi darrel,

An ArrayList initially takes up more memory than an array. However, resizing
an array means making a whole new array. I'm sure you can see how a few
resizes would tip the balance in favor of a Collection or List.

It's a good thing, though, that you're paying attention to optimization
issues!

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top