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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I Raise Event when a collection is Terminated?

Status
Not open for further replies.

DavidLogue

Programmer
Apr 17, 2003
4
IE
Hi,

I have defined two events in a collection as follows...

Public Event NewObject(mcount As Integer)
Public Event KillObject(mcount As Integer)
Private mCol As Collection

These are raised in the class (collection) at
1) Public Function Add(...
...
If Len(sKey) = 0 Then
mCol.Add objNewMember
Else
mCol.Add objNewMember, sKey
End If

'return the object created
Set Add = objNewMember
Set objNewMember = Nothing
RaiseEvent NewObject(COUNT)

2) Private Sub Class_Terminate()
'destroys collection when this class is terminated
Set mCol = Nothing
RaiseEvent KillObject(0)
End Sub

Then in a form I Raise the 'NewObject' event when I add a new member to the collection. This works fine.

Private Sub colCollection_NewObject(mcount As Integer)
StatusBar1.Panels(1).Text = mcount
End Sub

My problem is that I can't raise the 'Kill Object' Event when I set the instance of the collection to Nothing.

Private Sub colCollection_KillObject(mcount As Integer)
StatusBar1.Panels(1).Text = ""
End Sub

Do you have any ideas how I could raise an event when I destroy a collection?
 
You generally don't want to do significant work when destroying an object -- the general idea is just to clean stuff up and get out as fast as you can.

What you could do is wrapper your collection class in another class. The wrapper class would have a method (not a destructor) that would free up the collection class, plus raise your event. Since the event isn't being raised in a destructor, it should work. The downside is that your callers now have to explicitly call a method to free it, rather than allowing it to go out of scope automatically. Which isn't so bad -- when you do file I/O, you should call the .Close method when you're done writing, and this isn't too different from that.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top