How can I tell if something is an event based on its signature?

Viewed 35

I recently assumed that because the name of a function occurred nowhere in the codebase other than its definition, it must be dead code. I later found out it was actually an event, and therefore not dead. Its name and signature was Private Sub lstFooWeekly_Change(). Now that I know about the Change() event, it's pretty obvious that this code wasn't dead. In future, I'd like to be able to spot events ahead of time.

My question is this: Given the signature of something that looks like a function, how can I tell if it's an event?

Note: I'm a total VBA novice. I've tried searching the documentation for any answer to my question, but all that I've found is documentation for specific events. It seems that the general pattern is "if it's Private Sub and has _ in its name, then it's an event", but I can't trust that without an official source. Furthermore, I'd much prefer an "if and only if" solution to my "if" guess. If what I'm looking for is documented anywhere, I'd appreciate a link.

1 Answers

Maybe this "dummy" example helps to clarify. Let's create two classes

clsRaise

Public Event Dummy()

Public Sub raiseDummy()
    RaiseEvent Dummy
End Sub

clsSink

Public WithEvents my As clsRaise

Private Sub Class_Initialize()
    Set my = New clsRaise
End Sub

Private Sub Class_Terminate()
    my.raiseDummy
End Sub

Sub my_dummy()
    MsgBox "Oh no Risen from the code"
End Sub

Just step through the code by pressing F8 instead of F5 and watch

Sub TestDummy()
    Dim mD As clsSink
    Set mD = New clsSink
End Sub

Also have a look at the object browser for the VBA project and you will see that in the clsRaise you have the event Dummy and in the class clsSink you have the sub my_dummy which implements the event. The rule for the naming of this sub is <variable name>_<eventname>. As you can also name any procedure with an underscore you cannot really tell just by the name if a procedure implements an event.

enter image description here

enter image description here

Further reading. Hope this makes sense

Related