shared events possible?

B

Boni

Dear all,

is it not possible to have shred events?

Thanks,

Boni

error BC30600: 'WithEvents' variable does not raise any instance events that
are accessible to 'Class A."





Friend Class A

Public Shared WithEvents MyEvents As New cMyEvents

End Class

Class cMyEvents

Public Shared Event MyStarEvent

Shared Sub Raisestart()

RaiseEvent MyStarEvent()

End Sub

End Class
 
L

Larry Lard

Boni said:
Dear all,

is it not possible to have shred events?

I think it is.
error BC30600: 'WithEvents' variable does not raise any instance events that
are accessible to 'Class A."

The error message is actually useful here. If you declare an object
*variable* WithEvents, that means that it is going to hold an
*instance* of that class, so there must be *instance* events for it to
listen for. cMyEvents doesn't have any *instance* events so object
variables of type cMyEvents will never receive any events, so can't be
declared WithEvents.

That make sense? :)

Your event class definition is fine:

Class cMyEvents

Public Shared Event MyStarEvent()

Shared Sub Raisestart()
RaiseEvent MyStarEvent()
End Sub
End Class

So now we have defined a shared event, cMyEvents.MyStarEvent. We just
need to set up a handler, so let's try:

Friend Class A
Public Shared Sub HandleStarEvent() Handles cMyEvents.MyStarEvent

End Sub
End Class

But this doesn't compile - it seems that 'Handles', like 'WithEvents',
is only to be used for *instance* event handling. But we are not done
yet! Now we try:

Friend Class A

Public Shared Sub HandleStarEvent()

End Sub

Public Shared Sub StartListening()
AddHandler cMyEvents.MyStarEvent, AddressOf HandleStarEvent
End Sub

End Class

and this works! You must of course remember to call StartListening at
some point :)
 
B

Boni

Thanks for great explantion Larry!
Larry Lard said:
I think it is.


The error message is actually useful here. If you declare an object
*variable* WithEvents, that means that it is going to hold an
*instance* of that class, so there must be *instance* events for it to
listen for. cMyEvents doesn't have any *instance* events so object
variables of type cMyEvents will never receive any events, so can't be
declared WithEvents.

That make sense? :)

Your event class definition is fine:

Class cMyEvents

Public Shared Event MyStarEvent()

Shared Sub Raisestart()
RaiseEvent MyStarEvent()
End Sub
End Class

So now we have defined a shared event, cMyEvents.MyStarEvent. We just
need to set up a handler, so let's try:

Friend Class A
Public Shared Sub HandleStarEvent() Handles cMyEvents.MyStarEvent

End Sub
End Class

But this doesn't compile - it seems that 'Handles', like 'WithEvents',
is only to be used for *instance* event handling. But we are not done
yet! Now we try:

Friend Class A

Public Shared Sub HandleStarEvent()

End Sub

Public Shared Sub StartListening()
AddHandler cMyEvents.MyStarEvent, AddressOf HandleStarEvent
End Sub

End Class

and this works! You must of course remember to call StartListening at
some point :)
 
H

Herfried K. Wagner [MVP]

Boni said:
is it not possible to have shred events?

In addition to the other reply, it's possible to use the 'AddHandler'
statement to add a handler to a shared event.
 

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