dynamic arrays in vb.net ?????

im trying to declare an array with an unknown length and no way of figuring it out untill the actual data has been placed within the array, in c++ it possible to fill a dynamic array with data and then find out the length if u need 2, but in vb.net im not sure, i know abt the redim concept, but does it work in this case...

thank u sooo much !!!

Comments

  • seancampbellseancampbell Pennsylvania, USA
    You a few options to solve the scenario you are facing.

    Firstly, and more directly to your questions, you can declare the size of an array dynamically like this:

    [code]
    'Say I am getting data from the database, and i don't know how
    'many rows I am getting...

    'At somepoint, I will get a count for the amount of data coming back, and when I get that 'count' i can declare the array like so:

    Dim RowCount as Integer = 15

    Dim DynamArray(RowCount) As Integer
    'Now DynamArray's bounds are 0 - 14

    'You can also declare an array with no size:
    Dim DynamArray() as Integer

    'Then change the length afterwards:
    ReDim DynamArray(RowCount)

    'And Finally, if you want to add an item to the array:
    ReDim Preserve DynamArray(DynamArray.Length)
    DynamArray(DynamArray.Length - 1) = Value
    [/code]

    Since you understand how to do this enough to have done it in C++, I won't bore you with details... Here is another way to do it, look into using an arrayList, it dynamically holds multiple datatypes

    Dim aList As New ArrayList
    aList.Add(Object)
    aList.Count
    aList.Item(0)
  • thank u for the info, really helped, im still new to vb.net so im kinda learning as i go, thanx again!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories