Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WithEvents not Working Inside Own Class Module

Status
Not open for further replies.

TomHW

Programmer
Sep 8, 2004
100
US
I have created a class module that has its own events. If I create another class, I can use WithEvents to hook into those events. If I create an instance of the first class inside of the first class, in this case a kind of pointer to the next instance in the linked list, I am not able to use WithEvents and hook into the second instances events.

The error I receive is this:
Cannot handle events for the object specified.

These events can be handled in other class modules, but not in its own. Does anyone have any ideas why this occurs or how it can be corrected?

Thanks,
Tom
 
My gut instinct is that you're getting into a recursive cascade if you try to instantiate an instance of a class within that same class. My usual approach is to create a collection class that contains a collection of objects of of the class that has multiple instances. I then use the "Add", "Remove", "Item" and "Enum" methods / properties of the collection to move through the various instances of the class.
 
I understand your idea, but that doesn't seem to be the case here. I have created linked lists before in VBA with no issues. In fact, this one works quite well aside from not being able to Hook into events. If I change the code:

Private WithEvents mobjNext as cListItem

to:

Private mobjNext as cListItem

the list works. The linked list idea was supposed to allow me to have each node hook into the events of the next node. Each node contains a control, so if a control anywhere in the list changes, that node can raise an event which would cause the node before it to raise an event, all the way down the chain. The class which holds the list would then take the appropriate actions and then pass information back through the list to every node. There is no way I know of to hook into events of objects inside of a collection.

As for the recursive cascade:
This will only occur if you instantiate the class inside of the original class when the original class initializes, which would cause that new class to instantiate a new class and so on. The class inside the original class is only instantiated when a new node is added to the list which means that the new class will not instantiate its interior class until another node is added.

Sorry to go on for so long, but this problem has been plaguing me for days.

Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top