6

I have a piece of code written in Visual Basic:

Dim n As Double, i As Integer
n = 4
Dim Ramp_length(1 To 4) As Double
For i = 1 To n
    Ramp_length(i) = Cells(13 + i, 5)
    'Cells(65 + i, 7) = Ramp_length(i)'
Next i

Is there a way I can reproduce the result without declaring the array with a "fixed" length? I eventually want the code to read off a column of data with unknown length and store it in an array of equal or smaller length so it can be altered. This is just a piece of the code...if someone could help me out that would be great! :D

Thanks

1
  • 1
    IS this VB, VBA, VB.NET? Please do specify which one. Commented Jan 15, 2010 at 0:06

5 Answers 5

3

You can use ReDim Preserve to resize an array. If you only need it a view times and don't use it in a loop, it's ok. But ReDim Preserve is very slow, because it creates a completely new array and copies the old values into it. If you need to fill an array up with an unknown amount of data sets, use a list. I made a small example:

Dim num_iterations as Integer

' redim solution
Dim dyn_array As Integer()
For i = 0 To num_iterations - 1
    ReDim Preserve dyn_array(i)
    dyn_array(i) = i
Next

' list solution
Dim list_for_array As New List(Of Integer)
For i = 0 To num_iterations - 1
    list_for_array.Add(i)
Next
Dim list_array As Integer() = list_for_array.ToArray

With 10000 iterations the ReDim-solution has a runtime of 32ms, the list solution is instantly ready (0 ms). 100000 iterations result in 5487ms using ReDim and 1ms using a list.

Sign up to request clarification or add additional context in comments.

Comments

2

Not sure if this is what you're looking for:

    Dim n As Double = 44
    Dim Ramp_Length_List As New List(Of Double)
    For i As Integer = 1 To n
        Ramp_Length_List.Add(Your_Temp_Double)
    Next i

    Dim Ramp_Length(Ramp_Length_List.Count - 1) As Double
    For i As Integer = 0 To Ramp_Length_List.Count - 1
        Ramp_Length(i) = Ramp_Length_List(i)
    Next
    Ramp_Length_List = Nothing
    'Ramp_Length() as Double is now sort of dynamic.

Comments

1
Dim DynaStrings() As String
ReDim DynaStrings(10)
DynaStrings(0) = "String 0"
'Need more data space!
ReDim DynaStrings(100)
DynaStrings(50) = "String 50"

Hope this might help.

1 Comment

So simple! You have to be aware that the number is the upper bound, not the array size. This is something obvious for people with a bit of knowledge about VB, but it wasn't for me :-)
0

There's no such a thing, array with "dynamic" length.

You can, however, encapsulate some logic that will resize the array whenever needed (this is known as ResizeArray in some languages, List in others, or ArrayList; I'm sorry, I don't know exactly which one in VB; if it's VB.NET then use "List").

Comments

0

You could use an ArrayList. It's the closest you can come to a dynamic array. There are other collections as well, so it may come down to what you expect to do with the data. However, you can't create an array and vary it's length dynamically in vb. (Well, not efficiently :) )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.