|
-
Nov 12th, 2008, 02:16 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Redim array of objects
I'm trying to create a dynamic array of objects
Code:
Private var() As New Entity
Private Sub Form_Load()
ReDim var(1 To 5) As Entity
End Sub
Compile error:
Can't change data types of array elements
Last edited by winterslam; Nov 12th, 2008 at 03:03 AM.
-
Nov 12th, 2008, 02:47 AM
#2
Re: Redim array of objects
Remove the bold line...
Code:
Private var() As New Entity
-
Nov 12th, 2008, 02:48 AM
#3
Re: Redim array of objects
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).
-
Nov 12th, 2008, 02:50 AM
#4
Thread Starter
Addicted Member
Re: Redim array of objects
Code:
Private var() As Entity
Private Sub Form_Load()
ReDim var(1 To 5) As Entity
var(1).x = 5
End Sub
Run-time error '91':
Object varialbe or With block variable not set
-
Nov 12th, 2008, 02:52 AM
#5
Re: Redim array of objects
The following also works...
Code:
Private var() As New Entity
Private Sub Form_Load()
ReDim var(1 To 5) As New Entity
End Sub
-
Nov 12th, 2008, 03:00 AM
#6
Thread Starter
Addicted Member
Re: Redim array of objects
 Originally Posted by dee-u
The following also works...
Code:
Private var() As New Entity
Private Sub Form_Load()
ReDim var(1 To 5) As New Entity
End Sub
thanks
it works
-
Nov 12th, 2008, 03:01 AM
#7
Re: Redim array of objects
 Originally Posted by winterslam
Code:
Private var() As Entity
Private Sub Form_Load()
ReDim var(1 To 5) As Entity
var(1).x = 5
End Sub
Run-time error '91':
Object varialbe or With block variable not set
You have to instantiate them first before you could use them...
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
-
Nov 12th, 2008, 03:12 AM
#8
Re: [RESOLVED] Redim array of objects
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.
Last edited by leinad31; Nov 12th, 2008 at 03:16 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|