473,480 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

VB 2005 Class Event Handlers

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.


Dec 13 '05 #1
7 1691
No, you cannot require that a program handle the event of a particular
component or control.

"Michael D. Ober" <obermd.@.alum.mit.edu.nospam> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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.

Dec 13 '05 #2
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
"Marina" <so*****@nospam.com> wrote in message
news:eT**************@TK2MSFTNGP09.phx.gbl...
No, you cannot require that a program handle the event of a particular
component or control.

"Michael D. Ober" <obermd.@.alum.mit.edu.nospam> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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.


Dec 13 '05 #3
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/arc...8/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
"Michael D. Ober" <obermd.@.alum.mit.edu.nospam> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
| 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.
|
|
|
|
Dec 13 '05 #4
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

"Michael D. Ober" <obermd.@.alum.mit.edu.nospam> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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.

Dec 14 '05 #5
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.
"Greg Burns" <bl*******@newsgroups.nospam> wrote in message
news:eG*************@TK2MSFTNGP14.phx.gbl...
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
"Marina" <so*****@nospam.com> wrote in message
news:eT**************@TK2MSFTNGP09.phx.gbl...
No, you cannot require that a program handle the event of a particular
component or control.

"Michael D. Ober" <obermd.@.alum.mit.edu.nospam> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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.




Dec 14 '05 #6
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
"Michael D. Ober" <obermd.@.alum.mit.edu.nospam> wrote in message
news:u0*************@tk2msftngp13.phx.gbl...
| 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.
|
|
| "Greg Burns" <bl*******@newsgroups.nospam> wrote in message
| news:eG*************@TK2MSFTNGP14.phx.gbl...
| > 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
| >
| >
| > "Marina" <so*****@nospam.com> wrote in message
| > news:eT**************@TK2MSFTNGP09.phx.gbl...
| > > No, you cannot require that a program handle the event of a particular
| > > component or control.
| > >
| > > "Michael D. Ober" <obermd.@.alum.mit.edu.nospam> wrote in message
| > > news:%2****************@TK2MSFTNGP10.phx.gbl...
| > >> 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.
| > >>
| > >>
| > >>
| > >>
| > >
| > >
| >
| >
| >
|
|
|
Dec 14 '05 #7

Michael D. Ober wrote:
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).OptionalCallbackProced ure
End If

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

--
Larry Lard
Replies to group please

Dec 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1440
by: Bingo | last post by:
Quick question, I'm new to C# and ASP.NET. Why do the OnInit, OnPreRender and other eventhandlers in the Page class only have (Eventargs) as the only argument? Isn't the signature of an...
8
1550
by: SStory | last post by:
When I right a class, I am wondering what are the best practices for error handling? Do I try..catch and trap the error and if so what do I do with it? Because most likely the class user will...
5
3557
by: Juan T. Llibre | last post by:
OK, guys, usually I answer questions instead of asking them, but this thing has me scratching my head. Why is the default for AutoEventWireup different for C# and VB.NET ? In VS 2005, if I...
2
2059
by: Mark | last post by:
Hi everyone, I am having a tinker around with Visual Studio Professional 2005 and I am building a simple website: Now, in 1.1 the event handlers were declared in the code behind page and you...
3
1348
by: Dino Buljubasic | last post by:
Hi, I am thinking of using visual inheritance. I am using C# VS 2005. I am concerned about how good it is. I have heard about problems like controls dissapearing or so. Any hints, tips,...
4
3240
by: Jonathan Wood | last post by:
I'm building a Web application but this question should be common to all C# applications. When I use a class, and I want to add event handlers or override base class methods, how do I know the...
5
1310
by: =?Utf-8?B?U2NhbmJveQ==?= | last post by:
Guyz, Whatever happened to the 'Index' property for a control, that used to be present in VB 3.0 / 4.0 / 5.0 / 6.0 and which now seems to be missing from VBE 2005? I need to be able to make 4...
2
1559
by: hharry | last post by:
Hello All, In VS 2003, I was able to add event handlers in the InitializeComponent function. In VS 2005, I can no longer see the auto- generated code. I tried to add the event handler for a...
5
1798
by: gnassar | last post by:
Essentially my problem is that .NET 2005 is removing my event handlers. There's no real special things about my project, it just continually removes them all. It starts on the open of a...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7076
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4471
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1294
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.