469,600 Members | 2,202 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,600 developers. It's quick & easy.

Unlimited Array in Visual Basic 8

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
?

Feb 25 '07 #1
9 3383
"Joe" <jo*****@tlen.plschrieb:
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)'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Feb 26 '07 #2
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" <jo*****@tlen.plwrote in message
news:11**********************@m58g2000cwm.googlegr oups.com...
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
?

Feb 26 '07 #3
Joe
On Feb 25, 6:46 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.atwrote:
"Joe" <joe1...@tlen.plschrieb:
You may want to use 'System.Collections.Generic.List(Of T)'.
ok, but later on, how can I read n-element from x element list (f.e. I
want to read 5th one?
Feb 26 '07 #4
On 2007-02-26, Joe <jo*****@tlen.plwrote:
On Feb 25, 6:46 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.atwrote:
>"Joe" <joe1...@tlen.plschrieb:
You may want to use 'System.Collections.Generic.List(Of T)'.

ok, but later on, how can I read n-element from x element list (f.e. I
want to read 5th one?

dim i as integer = theList(4)

HTH
--
Tom Shelton
Feb 26 '07 #5
Joe
On Feb 25, 8:22 pm, Tom Shelton <tom_shel...@comcastXXXXXXX.net>
wrote:
On 2007-02-26, Joe <joe1...@tlen.plwrote:
On Feb 25, 6:46 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.atwrote:
"Joe" <joe1...@tlen.plschrieb:
You may want to use 'System.Collections.Generic.List(Of T)'.
ok, but later on, how can I read n-element from x element list (f.e. I
want to read 5th one?

dim i as integer = theList(4)

HTH
--
Tom Shelton

thank you very much, everything clear now :)

Feb 26 '07 #6
Joe,

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

Miro

"Joe" <jo*****@tlen.plwrote in message
news:11**********************@h3g2000cwc.googlegro ups.com...
On Feb 25, 8:22 pm, Tom Shelton <tom_shel...@comcastXXXXXXX.net>
wrote:
>On 2007-02-26, Joe <joe1...@tlen.plwrote:
On Feb 25, 6:46 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.atwrote:
"Joe" <joe1...@tlen.plschrieb:
You may want to use 'System.Collections.Generic.List(Of T)'.
ok, but later on, how can I read n-element from x element list (f.e. I
want to read 5th one?

dim i as integer = theList(4)

HTH
--
Tom Shelton


thank you very much, everything clear now :)

Feb 26 '07 #7
"Miro" <mi******@golden.netwrote in news:u3sUI$aWHHA.4964
@TK2MSFTNGP06.phx.gbl:
If you are going to be using arrays, take a look at
REDIM and the PRESERVE command.
Redim Preserve is very inefficent so it is be better to use a Generic.List
as Herfried mentioned or an arraylist. Both can be converted to an array
using the ToArray function.

http://weblogs.asp.net/eporter/archi.../07/22943.aspx
Feb 26 '07 #8
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

"Herfried K. Wagner [MVP]" <hi***************@gmx.atschreef in bericht
news:%2***************@TK2MSFTNGP06.phx.gbl...
"Joe" <jo*****@tlen.plschrieb:
>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)'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Feb 26 '07 #9
Michel,

I am 100% sure that Herfried this knows.

Cor

"Michel Posseth [MCP]" <ms****@posseth.comschreef in bericht
news:up*************@TK2MSFTNGP05.phx.gbl...
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

"Herfried K. Wagner [MVP]" <hi***************@gmx.atschreef in bericht
news:%2***************@TK2MSFTNGP06.phx.gbl...
>"Joe" <jo*****@tlen.plschrieb:
>>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)'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>


Feb 26 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by HateSpam | last post: by
20 posts views Thread by samean | last post: by
7 posts views Thread by André | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.