I'm trying to create a dynamic array of objects
Compile error:Code:Private var() As New Entity
Private Sub Form_Load()
ReDim var(1 To 5) As Entity
End Sub
Can't change data types of array elements
Printable View
I'm trying to create a dynamic array of objects
Compile error:Code:Private var() As New Entity
Private Sub Form_Load()
ReDim var(1 To 5) As Entity
End Sub
Can't change data types of array elements
Remove the bold line...
Code:Private var() As New Entity
You're supposed to declare an array of references to objects (which can be Nothing) so don't instantiate with new, Dim var() As Entity. Set each array element with reference to class instance accordingly later on, Set var(0) = New Entity. When resizing with Redim just change upper and lower bounds, don't specify type which has already been declared (hence your error message).
Run-time error '91':Code:Private var() As Entity
Private Sub Form_Load()
ReDim var(1 To 5) As Entity
var(1).x = 5
End Sub
Object varialbe or With block variable not set
The following also works...
Code:Private var() As New Entity
Private Sub Form_Load()
ReDim var(1 To 5) As New Entity
End Sub
thanksQuote:
Originally Posted by dee-u
it works
You have to instantiate them first before you could use them...Quote:
Originally Posted by winterslam
Code:Private Sub Form_Load()
Dim a As Long
ReDim Var(1 To 5) As Entity
For a = LBound(Var) To UBound(Var)
Set Var(a) = New Entity
Next
Var(1).x = 5
End Sub
Your misconception is that in creating an array declared as Entity you have an array of objects/instances. What you have initially are just place holders. You only have an array of references (memory locations) that are initially set to Nothing (is zero or doesn't reference a memory location which implies that instance of class has not yet been created).
I don't see the point in declaring dynamic array As New since array elements don't exist.