30

Why am I unable to set the size of an array based on a variable? What's the best way around this?

Dim NumberOfZombies as integer
NumberOfZombies = 20000
Dim Zombies(NumberOfZombies) as New Zombie

5 Answers 5

59

You can use a dynamic array when you don't know the number of values it will contain until run-time:

Dim Zombies() As Integer
ReDim Zombies(NumberOfZombies)

Or you could do everything with one statement if you're creating an array that's local to a procedure:

ReDim Zombies(NumberOfZombies) As Integer

Fixed-size arrays require the number of elements contained to be known at compile-time. This is why you can't use a variable to set the size of the array—by definition, the values of a variable are variable and only known at run-time.

You could use a constant if you knew the value of the variable was not going to change:

Const NumberOfZombies = 2000

but there's no way to cast between constants and variables. They have distinctly different meanings.

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

Comments

4

You have to use the ReDim statement to dynamically size arrays.

Public Sub Test()
    Dim NumberOfZombies As Integer
    NumberOfZombies = 20000
    Dim Zombies() As New Zombie
    ReDim Zombies(NumberOfZombies)

End Sub

This can seem strange when you already know the size of your array, but there you go!

Comments

2

You can also look into using the Collection Object. This usually works better than an array for custom objects, since it dynamically sizes and has methods for:

  • Add
  • Count
  • Remove
  • Item(index)

Plus its normally easier to loop through a collection too since you can use the for...each structure very easily with a collection.

Comments

1

You need to use a constant.

CONST NumberOfZombies = 20000
Dim Zombies(NumberOfZombies) As Zombies

or if you want to use a variable you have to do it this way:

Dim NumberOfZombies As Integer
NumberOfZombies = 20000

Dim Zombies() As Zombies

ReDim Zombies(NumberOfZombies)

3 Comments

Is there a tidier way to cast NumberOfZombies as a CONST right in the dimension definition? Dim Zombies(CONST NumberOfZombies) as new Zombie
I submitted my answer before I was finished typing.
Oh, I see, so a variable can be used to define a dimension if Redim is used, that is helpful to know, thanks!
0

There is a stupid way to do it XD I am breaking an address into different cell by counting character.

Just show you directly.

q = 1
ReDim Sentence1(q) As String
ReDim Sentence1T(q) As String


Sentence1(q) = Result(0)

    For p = 1 To WordCount - 1
        Sentence1(q) = Sentence1(q) & " " & Result(p)
        
            If Len(Sentence1(q)) > 40 Then
            
                For r = 1 To q
                    Sentence1T(r) = Sentence1(r)
                Next r
                
                q = q + 1
                ReDim Sentence1(q) As String
                
                 For r = 1 To q - 1
                    Sentence1(r) = Sentence1T(r)
                Next r
                
                ReDim Sentence1T(q) As String
                
            End If
    Next p

So every time you redim it, you lost your data. Just put it in another array. Hope you like it!

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.