1

In VB script, I'm trying to resize an array inside of an object (named itemList()). ReDim works for normal arrays, but I get an error when resizing an array inside of an object. I'm trying to imitate a struct, so my goal is to have some type of object/struct that has a dynamic array inside..

Class Person
    Public name
    Dim itemList()
End Class 

Set person1 = new Person
person1.itemList(0) = "football"    'Works fine
ReDim person1.itemList(7)           'Error: "Expected "("
0

1 Answer 1

4

You cannot resize member variables of an object that way. A better approach to handling a list of items is to initialize the member variable as an empty array and append to it:

Class Person
    Public name
    Public itemList

    Private Sub Class_Initialize()
        itemList = Array()
    End Sub

    Public Sub Take(item)
        ReDim Preserve itemList(UBound(itemList)+1)
        itemList(UBound(itemList)) = item
    End Sub
End Class 

Set person1 = new Person
person1.Take "football" 
Sign up to request clarification or add additional context in comments.

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.