473,732 Members | 2,219 Online
Bytes | Software Development & Data Engineering Community
+ 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(p arms)
Event RequiredEvent(p arms)

end Class

Mike Ober.


Dec 13 '05 #1
7 1715
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******** ********@TK2MSF TNGP10.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(p arms)
Event RequiredEvent(p arms)

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(B yVal e As EventArgs)
End Interface

Public Class Class1
Implements IRequiredEvent
Public Event OptionEvent(ByV al e As System.EventArg s)
Public Event RequiredEvent(B yVal e As System.EventArg s) Implements
IRequiredEvent. RequiredEvent
End Class

Public Class Class2
Implements IRequiredEvent
Public Event RequiredEvent(B yVal e As System.EventArg s) Implements
IRequiredEvent. RequiredEvent
End Class

Greg
"Marina" <so*****@nospam .com> wrote in message
news:eT******** ******@TK2MSFTN GP09.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******** ********@TK2MSF TNGP10.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(p arms)
Event RequiredEvent(p arms)

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_requiredEvent Handlers As EventHandler

Public Custom Event RequiredEvent As EventHandler
AddHandler(ByVa l value As EventHandler)
m_requiredEvent Handlers =
DirectCast([Delegate].Combine(m_requ iredEventHandle rs, value), EventHandler)
End AddHandler

RemoveHandler(B yVal value As EventHandler)
m_requiredEvent Handlers =
DirectCast([Delegate].Remove(m_requi redEventHandler s, value), EventHandler)
End RemoveHandler

RaiseEvent(ByVa l sender As Object, ByVal e As System.EventArg s)
If m_requiredEvent Handlers Is Nothing Then
Throw New InvalidOperatio nException("No event handlers
attached")
End If
m_requiredEvent Handlers.Invoke (sender, e)
End RaiseEvent
End Event

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

In VB 2002 & 2003 you can use the hidden field RequiredEventEv ent 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 RequiredEventEv ent Is Nothing Then
Throw New InvalidOperatio nException("No event handlers
attached")
End If
RaiseEvent RequiredEvent(M e, 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******** ********@TK2MSF TNGP10.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(p arms)
| Event RequiredEvent(p arms)
|
| 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 IUsersMustImple mentThisInterfa ce
Sub Method1()
Sub Method2()
End Interface

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

/claes

"Michael D. Ober" <obermd.@.alum. mit.edu.nospam> wrote in message
news:%2******** ********@TK2MSF TNGP10.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(p arms)
Event RequiredEvent(p arms)

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(requir edexternalhandl er)

where

Public Function RequiredExterna lHandler(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
ConsoleApplicat ion, Windows Forms Application, and ASPNet Application.

Thanks,
Mike.
"Greg Burns" <bl*******@news groups.nospam> wrote in message
news:eG******** *****@TK2MSFTNG P14.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(B yVal e As EventArgs)
End Interface

Public Class Class1
Implements IRequiredEvent
Public Event OptionEvent(ByV al e As System.EventArg s)
Public Event RequiredEvent(B yVal e As System.EventArg s) Implements
IRequiredEvent. RequiredEvent
End Class

Public Class Class2
Implements IRequiredEvent
Public Event RequiredEvent(B yVal e As System.EventArg s) Implements
IRequiredEvent. RequiredEvent
End Class

Greg
"Marina" <so*****@nospam .com> wrote in message
news:eT******** ******@TK2MSFTN GP09.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******** ********@TK2MSF TNGP10.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(p arms)
Event RequiredEvent(p arms)

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(Addre ssOf RequiredExterna lHandler)

Where:

Class Something
Public Delegate Sub EventHandler(By Val 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 RequiredExterna lHandler(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.EventHan dler 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(ByV al e As System.EventArg s)
| > Public Event RequiredEvent(B yVal e As System.EventArg s)

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******** *****@tk2msftng p13.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(requir edexternalhandl er)
|
| where
|
| Public Function RequiredExterna lHandler(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
| ConsoleApplicat ion, Windows Forms Application, and ASPNet Application.
|
| Thanks,
| Mike.
|
|
| "Greg Burns" <bl*******@news groups.nospam> wrote in message
| news:eG******** *****@TK2MSFTNG P14.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(B yVal e As EventArgs)
| > End Interface
| >
| > Public Class Class1
| > Implements IRequiredEvent
| > Public Event OptionEvent(ByV al e As System.EventArg s)
| > Public Event RequiredEvent(B yVal e As System.EventArg s) Implements
| > IRequiredEvent. RequiredEvent
| > End Class
| >
| > Public Class Class2
| > Implements IRequiredEvent
| > Public Event RequiredEvent(B yVal e As System.EventArg s) Implements
| > IRequiredEvent. RequiredEvent
| > End Class
| >
| > Greg
| >
| >
| > "Marina" <so*****@nospam .com> wrote in message
| > news:eT******** ******@TK2MSFTN GP09.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******** ********@TK2MSF TNGP10.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(p arms)
| > >> Event RequiredEvent(p arms)
| > >>
| > >> 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(p arms)
Event RequiredEvent(p arms)

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 ICanBeCalledBac k
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(call er As ICanBeCalledBac k, ' ... the other parameters
caller.ProcessS tarted
'the process
caller.ProcessF inished
End Sub

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

Class ACaller
Implements ICanBeCalledBac k

Sub Started() Implements ICanBeCalledBac k.ProcessStarte d
'whatever
End Sub

Sub Finished() Implements ICanBeCalledBac k.ProcessFinish ed
'whatever
End Sub

Sub MakeTheCall
Dim tc As New TheClass

tc.TheProcess(M e)
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 ICanBeCalledBac kOptionally Then
DirectCast(call er,
ICanBeCalledBac kOptionally).Op tionalCallbackP rocedure
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
1450
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 event handler supposed to be (object, eventArgs)? My only guess is that these aren't being assigned via a delegate, but
8
1570
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 want to know the information in the exception.... That being the case do I just not catch it and let the user of the class catch it and get all the information? I know I could catch it and throw my own, but I'd have to tell them the same
5
3568
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 create a new VB website using code-behind, the following Page directive is inserted : <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
2
2071
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 could add in your own custom event handlers there e.g. #region Web Form Designer generated code override protected void OnInit(EventArgs e)
3
1359
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, ideas, concerns ??? Thank you, _dino_
4
3250
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 correct syntax for those methods? In MFC, I can simply choose event handlers from a list and they are inserted automatically with the correct signature. Similar deal with Visual Basic 6. But I can't seem to find anything like this in C#.
5
1325
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 command buttons behave in the same way, the 'Index' property indicating which command button I clicked on.
2
1575
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 button in the Page_Load event, bit this did not work. protected void Page_Load(object sender, EventArgs e)
5
1809
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 solution. The screen flashes and immediately I can undo something. I look at the undo list and it says designer generated code. When I undo it, all of my event handlers come back. This is a very annoying issue has anyone solved this problem? -G
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8774
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9307
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9181
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6031
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
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 we have to send another system
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.