VB 2005 Class Event Handlers

M

Michael D. Ober

Is there anyway to raise an event from a class and require that any program
using that class (not just inheritance) have an event handler for specific
events in the class? In my case, some events don't need to be handled, but
some must be handled by the program that uses the class.

I'm looking for something along the lines of

Class MyClass
Event OptionalEvent(parms)
Event RequiredEvent(parms)

end Class

Mike Ober.
 
M

Marina

No, you cannot require that a program handle the event of a particular
component or control.
 
G

Greg Burns

Can't you do something using implemention inheritance? Not my strongest
skill, so I not sure how you "require" that a class implement another.

Public Interface IRequiredEvent
Event RequiredEvent(ByVal e As EventArgs)
End Interface

Public Class Class1
Implements IRequiredEvent
Public Event OptionEvent(ByVal e As System.EventArgs)
Public Event RequiredEvent(ByVal e As System.EventArgs) Implements
IRequiredEvent.RequiredEvent
End Class

Public Class Class2
Implements IRequiredEvent
Public Event RequiredEvent(ByVal e As System.EventArgs) Implements
IRequiredEvent.RequiredEvent
End Class

Greg
 
J

Jay B. Harlow [MVP - Outlook]

Michael,
As Marina suggests you cannot require a program handle a particular event,
per se.

You could however throw an exception when you go to raise the event, if
there are no event handlers attached to it.

In VB 2005 I would rely on Custom Events to raise the exception.

http://www.panopticoncentral.net/archive/2004/08/06/1545.aspx

Something like:

Public Class MyClass

Private m_requiredEventHandlers As EventHandler

Public Custom Event RequiredEvent As EventHandler
AddHandler(ByVal value As EventHandler)
m_requiredEventHandlers =
DirectCast([Delegate].Combine(m_requiredEventHandlers, value), EventHandler)
End AddHandler

RemoveHandler(ByVal value As EventHandler)
m_requiredEventHandlers =
DirectCast([Delegate].Remove(m_requiredEventHandlers, value), EventHandler)
End RemoveHandler

RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
If m_requiredEventHandlers Is Nothing Then
Throw New InvalidOperationException("No event handlers
attached")
End If
m_requiredEventHandlers.Invoke(sender, e)
End RaiseEvent
End Event

Protected Overridable Sub OnRequiredEvent(ByVal e As EventArgs)
RaiseEvent RequiredEvent(Me, e)
End Sub


End Class

In VB 2002 & 2003 you can use the hidden field RequiredEventEvent to see if
there are any handlers or not.

Public Class MyClass

Public Event RequiredEvent As EventHandler

Protected Overridable Sub OnRequiredEvent(ByVal e As EventArgs)
If RequiredEventEvent Is Nothing Then
Throw New InvalidOperationException("No event handlers
attached")
End If
RaiseEvent RequiredEvent(Me, e)
End Sub

End Class

NOTE: In either case I normally follow the .NET event standard & define &
use the OnRequiredEvent sub to raise the event itself.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Is there anyway to raise an event from a class and require that any
program
| using that class (not just inheritance) have an event handler for specific
| events in the class? In my case, some events don't need to be handled,
but
| some must be handled by the program that uses the class.
|
| I'm looking for something along the lines of
|
| Class MyClass
| Event OptionalEvent(parms)
| Event RequiredEvent(parms)
|
| end Class
|
| Mike Ober.
|
|
|
|
 
C

Claes Bergefall

Define an interface that all users of your class must implement
Ensure that they do so by requiring a reference to it to be passed to the
constructor

Public Interface IUsersMustImplementThisInterface
Sub Method1()
Sub Method2()
End Interface

Public MyCoolClass
Public Sub New(ByVal user As IUsersMustImplementThisInterface)
' Keep a reference to the user
End Sub
End Class

/claes
 
M

Michael D. Ober

OK, given that I can't force an event handler to be used, what is the syntax
to do the following:

dim MyClass as new MyObject(requiredexternalhandler)

where

Public Function RequiredExternalHandler(byval unhandledvalue as string) as
string
end function

Basically I have a class that is using application specific login (uses the
integrated windows login most of the time), but when it fails, it needs to
call a platform specific login hander. The platforms are
ConsoleApplication, Windows Forms Application, and ASPNet Application.

Thanks,
Mike.
 
J

Jay B. Harlow [MVP - Outlook]

Michael,
| OK, given that I can't force an event handler to be used, what is the
syntax
| to do the following:

Dim anObject As New Something(AddressOf RequiredExternalHandler)

Where:

Class Something
Public Delegate Sub EventHandler(ByVal parms As Object)

Event OptionalEvent As EventHandler
Event RequiredEvent As EventHandler

Public Sub New(ByVal handler As EventHandler)
AddHandler RequiredEvent, handler
End Sub

End Class

Public Sub RequiredExternalHandler(ByVal parms As Object)

End Sub

Note Event handlers cannot be functions. You can change the "Delegate Sub
EventHandler" to include the correct parameters as needed.

I normally use System.EventHandler rather then define my own, unless I need
a specific type derived from EventArgs.

Using:
Event OptionalEvent As EventHandler
Event RequiredEvent As EventHandler
Instead of:
| > Public Event OptionEvent(ByVal e As System.EventArgs)
| > Public Event RequiredEvent(ByVal e As System.EventArgs)

Is generally better as the first uses a predefined delegate (EventHandler)
where as the second will create a hidden delegate for each event that you
define, seeing as the events have the signuture you are getting a number of
identical duplicate types defined in your assembly...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| OK, given that I can't force an event handler to be used, what is the
syntax
| to do the following:
|
| dim MyClass as new MyObject(requiredexternalhandler)
|
| where
|
| Public Function RequiredExternalHandler(byval unhandledvalue as string) as
| string
| end function
|
| Basically I have a class that is using application specific login (uses
the
| integrated windows login most of the time), but when it fails, it needs to
| call a platform specific login hander. The platforms are
| ConsoleApplication, Windows Forms Application, and ASPNet Application.
|
| Thanks,
| Mike.
|
|
| | > Can't you do something using implemention inheritance? Not my strongest
| > skill, so I not sure how you "require" that a class implement another.
| >
| > Public Interface IRequiredEvent
| > Event RequiredEvent(ByVal e As EventArgs)
| > End Interface
| >
| > Public Class Class1
| > Implements IRequiredEvent
| > Public Event OptionEvent(ByVal e As System.EventArgs)
| > Public Event RequiredEvent(ByVal e As System.EventArgs) Implements
| > IRequiredEvent.RequiredEvent
| > End Class
| >
| > Public Class Class2
| > Implements IRequiredEvent
| > Public Event RequiredEvent(ByVal e As System.EventArgs) Implements
| > IRequiredEvent.RequiredEvent
| > End Class
| >
| > Greg
| >
| >
| > | > > No, you cannot require that a program handle the event of a particular
| > > component or control.
| > >
| > > | > >> Is there anyway to raise an event from a class and require that any
| > >> program
| > >> using that class (not just inheritance) have an event handler for
| > >> specific
| > >> events in the class? In my case, some events don't need to be
handled,
| > >> but
| > >> some must be handled by the program that uses the class.
| > >>
| > >> I'm looking for something along the lines of
| > >>
| > >> Class MyClass
| > >> Event OptionalEvent(parms)
| > >> Event RequiredEvent(parms)
| > >>
| > >> end Class
| > >>
| > >> Mike Ober.
| > >>
| > >>
| > >>
| > >>
| > >
| > >
| >
| >
| >
|
|
|
 
L

Larry Lard

Michael said:
Is there anyway to raise an event from a class and require that any program
using that class (not just inheritance) have an event handler for specific
events in the class? In my case, some events don't need to be handled, but
some must be handled by the program that uses the class.

I'm looking for something along the lines of

Class MyClass
Event OptionalEvent(parms)
Event RequiredEvent(parms)

end Class

That's not really what events are for, so any solution is going to be
kludgey at best. When you find yourself trying to work against the
framework, it's time to reconsider.

What it looks like you want is that any caller of a method (methods
have callers, not classes per se) has a way for you to call it back.
There are loosely speaking two ways to enforce a behavioural contract,
subclassing and interface implementation. The latter is the more
flexible so we'll go with that.

First make a list of the methods you are going to require your callers
to implement; put these all in an interface:

Interface ICanBeCalledBack
Sub ProcessStarted()
Sub ProcessFinished()
End Interface

Now, in each method that is going to call its caller back, accept a
parameter of this interface type:

Class TheClass 'avoid MyClass even in examples as it is a keyword :)

Sub TheProcess(caller As ICanBeCalledBack, ' ... the other parameters
caller.ProcessStarted
'the process
caller.ProcessFinished
End Sub

Finally, in an object that is going to call this method, implement the
interface:

Class ACaller
Implements ICanBeCalledBack

Sub Started() Implements ICanBeCalledBack.ProcessStarted
'whatever
End Sub

Sub Finished() Implements ICanBeCalledBack.ProcessFinished
'whatever
End Sub

Sub MakeTheCall
Dim tc As New TheClass

tc.TheProcess(Me)
End Sub
End Class

Now the compiler will enforce for you that any caller of TheProcess
must pass as the 'caller' an objet that *definitely* implements the
callback procedures defined in the interface.

You can do the optional callback stuff similarly - define an interface
for the optional callbacks, then in TheProcess do

If TypeOf caller Is ICanBeCalledBackOptionally Then
DirectCast(caller,
ICanBeCalledBackOptionally).OptionalCallbackProcedure
End If

Please note that Claes has already given you this answer in this thread
but I thought a little more motivation might help.
 

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