Unlimited Array in Visual Basic 8

  • Thread starter Thread starter Joe
  • Start date Start date
J

Joe

hi. simple question: how can one declare Array (or table) with unknown
number of elements, and then assign values to it, example:

Dim TableStrings() as String

TableStrings(0) = "first value"
TableStrings(1) = "second"
TableStrings(2) = "tird" .... etc


?
 
Joe said:
hi. simple question: how can one declare Array (or table) with unknown
number of elements, and then assign values to it, example:

Dim TableStrings() as String

TableStrings(0) = "first value"
TableStrings(1) = "second"
TableStrings(2) = "tird" .... etc

You may want to use 'System.Collections.Generic.List(Of T)'.
 
Joe,

I'm a very fresh beginner to VB.Net, so hopefully I won't screw this up, but
what you need to do is declare the array as you have in your example and add
a counter to loop through each value and assign it to the count value. In
psuedo-code it would look something like...

Declare your counter variable
Declare your array
For Each element in the input data
Assign the values to the array elements (Table(counter))
'Do other processing if necessary'
Increment the counter by 1 (counter += 1)
Next element

Of course, if you provided information on exactly how you are getting the
information to place in the array, others more knowledgeable than I would be
able to provide you with some more concrete information.

Bruce
 
Joe,

If you are going to be using arrays, take a look at
REDIM and the PRESERVE command.

Miro
 
or take a look at the array class


Dim x(1) As Integer ' create instance with one element

For i As Integer = 1 To 10

Array.Resize(x, x.Length + i) ' add some elements during runtime

Next

' to proof that it works

For Each i As Integer In x

Debug.WriteLine(i.ToString) ' writes out all zero`s for every element

Next

ofcourse you could do the above example with anny type you want

regards

Michel Posseth
 
Michel,

I am 100% sure that Herfried this knows.

Cor

Michel Posseth said:
or take a look at the array class


Dim x(1) As Integer ' create instance with one element

For i As Integer = 1 To 10

Array.Resize(x, x.Length + i) ' add some elements during runtime

Next

' to proof that it works

For Each i As Integer In x

Debug.WriteLine(i.ToString) ' writes out all zero`s for every element

Next

ofcourse you could do the above example with anny type you want

regards

Michel Posseth
 
Back
Top