property Let in var() of objects

  • Thread starter Thread starter Dick Watson
  • Start date Start date
D

Dick Watson

This works:
Set objMyObject = New MyObject
objMyObject.MyProperty = 1

This doesn't work, reports Object doesn't support this method or property
Set objMyObjectArray(lngPtr) = New MyObject
objMyObjectArray(lngPtr).MyProperty = 1

Why not? What am I missing? (Probably something really stupid, but I'm
approaching a mental vapor lock on this.) If I look in the watch window, I
clearly see the object get created in the array by the Set. I see it's
MyProperty property. The assign to the property blows up.
 
Hi Dick,

FWIW this works as expected when I try it (though I wouldn't recommend
mixing the objects this way<g>):

Public Sub TestObjArray()
Dim A(9) As Variant
'or
'Dim A(9) As Object

Set A(0) = New RegExp
A(0).Pattern = "Now is the time"
Set A(8) = CurrentDb.OpenRecordset("TheNumbers")
Debug.Print A(0).Pattern
Debug.Print A(0).Multiline
Debug.Print "Result = " & CStr(A(8).Fields(0).Value * 2)
End Sub

What happens if you use a Collection instead of an array?
 
You're certainly onto what I'm trying to do, though I only have a mix of two
object types. If you got it to work, I'm surely at a loss why it isn't
working here. I even tried this:

Set objMyObjectArray(lngPtr) = New MyObject
Set objTmp = objMyObjectArray(lngPtr)
objTmp.MyProperty = 1

With the same result: in the Watch window the objTmp looks just fine,
MyProperty and all. The assignment gags "Object doesn't support this
property or method." But still this works:

Set objMyObject = New MyObject
objMyObject.MyProperty = 1


I may have to try the Collection. I'm resisting because it's overkill and,
well, this just sure seems like it ought to work. I've tested with the same
result on both an Access2k and 03 machine.

Thanks for the help.
 
Here's one solution (probably the way it has to work):

Dim objMyObjectArray() As Object, _
objTmp as MyObject
....
Set objMyObjectArray(lngPtr) = New MyObject
Set objTmp = objMyObjectArray(lngPtr)
objTmp.MyProperty = 1

I'm guessing that this is effectively a type casting operation necessary
because objMyObjectArray(lngPtr) is just an Object which has no MyProperty.
Late binding and how it is handled in VBA seems to be the issue here.

What I was missing earlier comparing back to my testcase for MyObject is
that it was:

Dim objMyObject As MyObject
....
Set objMyObject = New MyObject
objMyObject.MyProperty = 1
 
What about
Dim objMyObjectArray() As MyObject
?

Here's one solution (probably the way it has to work):

Dim objMyObjectArray() As Object, _
objTmp as MyObject
...
Set objMyObjectArray(lngPtr) = New MyObject
Set objTmp = objMyObjectArray(lngPtr)
objTmp.MyProperty = 1

I'm guessing that this is effectively a type casting operation necessary
because objMyObjectArray(lngPtr) is just an Object which has no MyProperty.
Late binding and how it is handled in VBA seems to be the issue here.

What I was missing earlier comparing back to my testcase for MyObject is
that it was:

Dim objMyObject As MyObject
...
Set objMyObject = New MyObject
objMyObject.MyProperty = 1
 
That's precisely what I'm trying to avoid. Though I could re-factor the two
objects I store in this array to one common object, they are two different
objects that ultimately store the same answer two different ways. (One is
for the case where the answer is known--a pre-defined key pointer to a table
entry of some system data, and the other where the thing the key points to
has to be created as it doesn't already exist--a new record of user data.

Dim objMyObjectArray() As Object, _
objTmp as MyObjectIsKnown
....
If boolAnswerIsAlreadyKnown Then ' if MyProperty is known
Set objMyObjectArray(lngPtr) = New MyObjectA
Set objTmp = objMyObjectArray(lngPtr)
objTmp.MyProperty = lngPtrVal
Else '
Set objMyObjectArray(lngPtr) = New MyObjectB
objMyObjectArray(lngPtr) = objWeAreGoingToSolveForMyProperty
End If

The difficulty and ugliness of the solution at hand is probably trying to
tell me that I just need to bite the bullet and redo the two underlying
objects into one. Sometimes you get too invested in lower level code already
written and, when wrapping code around it gets too ugly, miss the hint that
the work already "done" (with test drivers and so on) just needs to be
redone to make it actually usable at the next level.
 
Back
Top