1

I am running into an error with the below code. When I debug, I find the line causing my problem:

Options(a) = New Element 

The error displayed is Object Variable or With Block Variable Not Set. With msg boxes I have found the value of a to be 0 at the time of the crash and the TotalItems to be 7. The Element object initialization is empty. I call the PopulateChildren method from another method within the same class. Am I using ReDim improperly? It seems like maybe it isn't increasing the size of my array... I have noticed examples of using it like this...

ReDim Preserve Options(0 to TotalItems)

...but it doesn't seem to do any different when I try it. Anyone have any idea what is going on?

Dim Options() As Element
Dim TotalItems As Integer
Dim Children(100) As Integer

Private Sub PopulateChildren()
    ReDim Preserve Options(TotalItems)
    For a = 0 To TotalItems - 1
        Options(a) = New Element
        Options(a).Populate (Children(a))
    Next a
End Sub

Thanks

2 Answers 2

4

Since Element is Object, you should use Set:

Set Options(a) = New Element
Sign up to request clarification or add additional context in comments.

4 Comments

And if you are worried about whether your array indexing starts at 0 or 1, add Option Base 0 to the top of the module so you are sure that a=0 is a valid index.
@Floris, except when you use a Split it will always start from 0 whether you have Option Base 0 or Option Base 1. I know there is no Splitused here but just saying :)
I always specify my arrays lower/upper bounds explicitly in VBA. I've been burned way too many times. In fact, I've started even using lbound(arr) to ubound(arr) when iterating through an array, too
Bah, just found a bug from pre-doing-this-time because I did an 1 to ubound mistake. Good timing to post this comment, I guess..
0

easy, put your dim options inside the SUB, or ...

public dim options()

sub ...
'your code
end sub

of course options doesn't exit for your sub like you wrote it ! so the error showing...

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.