Problem using an object inside another object..

M

Michel S.

Hello !

In a form module, I create an instance of a class object and I
initialize this object.

Set obj01 = New clsObj01

Later in the same procedure I create some instances of another class,
Set obj0201 = New clsObj02
Set obj0202 = New clsObj02
Set obj0203 = New clsObj02

All these objects are created in the UserForm_Initialise procedure and
destroyed in the UserForm_Terminate proc.

They are also defined as global at the form module level
Private obj01 AS clsObj01
Private obj0201 AS clsObj02
Private obj0202 AS clsObj02
Private obj0203 AS clsObj02


Always in the UserForm_Initialize procedure, I set one of this second
class properties to the first object in all instances :

Set obj0201.obj = obj01
Set obj0202.obj = obj01
Set obj0203.obj = obj01


This second class has the correct Property Set procedure :

Public Property Set ExtObject (obj AS clsObj01)
Set mobj = obj
End Property

of course, mobj is defined global at module level :
Private mobj AS clsObj01


Tracing the code in the property Set procedure, I can query the
properties of mobj and the correct values are returned.


In the clsObj02 module, there's another method I call later that uses
one of the properties of mobj

Public Sub ...

If mobj.Complete = True Then ...

I get an error 91 on that line (undefined object variable), and the
local variables window shows that mobj is nothing.

Since obj01 still exists (the form is still displayed and it's
Terminate procedure never executed at that point), I don't understand
why this happens..

Any idea ?
Thanks

NB: I also tried to use "Private mobj AS *New* clsObj01" in clsObj02,
but it doesn't make any difference, and IMHO it is not required because
it is not a new object, but a reference to an existing object.
 
M

Michel S.

I have additional info following some test I made :

I added a property "Name" to clsObj01, and defined it in the formModule

Set obj01 = New clsObj01
obj01.Name = "Form Module Instance"

Later, just after I passed the object to the other class instances
Set obj0201.obj = obj01
Set obj0202.obj = obj01
Set obj0203.obj = obj01

I put this code :

Debug.Print obj0201.obj.Name (result: "Form Module Instance")
Debug.Print obj0202.obj.Name (result: "")
Debug.Print obj0203.obj.Name (result: "")

Now I can see why I get the error 91 when using the object in instances
02 and 03 of clsObj02, but I still no not understand why the actual
obj01 reference is not kept inside obj0202 and obj0203..

Still looking for ideas or explanation..
 
M

Michel S.

I just found the problem : it is sitting on my chair !

Too much cut & paste, not enough coffee.. Well, it proves sometimes
you have to take a little break to get a clearer mind - and see the
obvious.

Apologies to all..
MS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top