473,322 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Mustoverride Event?

All,

Is there a way to force a derived class to contain an event. I have done
this with methods (i.e. Protected MustOverride Sub Test), but can I do
something similar with an event (Protected MustOverride Event MyEvent
doesn't work for obvious reasons)?

Thanks in advance.
Dave
Nov 20 '05 #1
8 2419
Dave,

I don't think it is possible to force a derived class to override an event.
You can of course make them override a protected method that is used in the
base class to raise the event. E.g.

-------------------
*Base Class*

Public Event NameChanged(sender as Object, e as EventArgs)

Protected Mustoverride Sub OnNameChanged(e as EventArgs)

Private Sub RaiseNameChanged()
me.OnNameChanged(EventArgs.Empty)
raiseevent NameChanged(me, EventArgs.Empty)
End Sub

-------------------

When you want to raise the event, simply call RaiseNameChanged. This way,
you know that the derived class must handle OnNameChanged before the event
is raised.

Hope this helps,

Trev.

"Dave Wurtz" <da*******************@asdsoftware.com> wrote in message
news:3f********@news.splitrock.net...
All,

Is there a way to force a derived class to contain an event. I have done
this with methods (i.e. Protected MustOverride Sub Test), but can I do
something similar with an event (Protected MustOverride Event MyEvent
doesn't work for obvious reasons)?

Thanks in advance.
Dave

Nov 20 '05 #2
Codemonkey,
Protected Mustoverride Sub OnNameChanged(e as EventArgs)
Not the best name for the MustOverride method, as OnNameChanged is normally
for the method to raise the event, per the Event Usage Guidelines in the
Design Guidelines for CLass Library Developers.
Private Sub RaiseNameChanged()
me.OnNameChanged(EventArgs.Empty)
raiseevent NameChanged(me, EventArgs.Empty)
End Sub Making RaiseNameChanged private, how are derived classes suppose to call it?
Or did I miss read you?

Don't get me wrong, I see where you are heading with this, and I like the
idea!

Hope this helps
Jay

"Codemonkey" <hu*********@hotmail.com> wrote in message
news:O3****************@TK2MSFTNGP12.phx.gbl... Dave,

I don't think it is possible to force a derived class to override an event. You can of course make them override a protected method that is used in the base class to raise the event. E.g.

-------------------
*Base Class*

Public Event NameChanged(sender as Object, e as EventArgs)

Protected Mustoverride Sub OnNameChanged(e as EventArgs)

Private Sub RaiseNameChanged()
me.OnNameChanged(EventArgs.Empty)
raiseevent NameChanged(me, EventArgs.Empty)
End Sub

-------------------

When you want to raise the event, simply call RaiseNameChanged. This way,
you know that the derived class must handle OnNameChanged before the event
is raised.

Hope this helps,

Trev.

"Dave Wurtz" <da*******************@asdsoftware.com> wrote in message
news:3f********@news.splitrock.net...
All,

Is there a way to force a derived class to contain an event. I have done this with methods (i.e. Protected MustOverride Sub Test), but can I do
something similar with an event (Protected MustOverride Event MyEvent
doesn't work for obvious reasons)?

Thanks in advance.
Dave


Nov 20 '05 #3
Dave,
In addition to what Codemonkey stated:
Is there a way to force a derived class to contain an event. A derived class will always contain (expose) the event, unfortunately there
is no real way to force a derived class to raise an event. What we normally
do is give derived classes the opportunity to raise the base classes events,
then take it on faith they will use it appropriately.

The Event Usage Guidelines in the Design Guidelines for Class Library
Developers, has defined a pattern that allows derived classes to raise an
event of a base class.

http://msdn.microsoft.com/library/de...Guidelines.asp

Public Class Base

Public Event NameChanged(sender as Object, e as EventArgs)

Protected Overrideable Sub OnNameChanged(e as EventArgs)
RaiseEvent NameChanged(me, e)
End Sub

Public Property Name() As String
...
Set(ByVal value As String)
...
OnNameChanged(EventArgs.Empty)
...
End Set
End Property

End Class

When ever the base class or derived classes need to raise the NameChanged
event they call the OnNameChanged method. Derived classes can override this
method to replace or supplement what happens when the NameChanged event
occurs. Which means if the derived classes still want the event to be raised
they need to use MyBase.OnNameChanged in their overridden OnNameChanged.

Which is similar to what Codemonkey showed. If the above does not address
your needs then a variation of what Codemonkey may be what you need.

Hope this helps
Jay

"Dave Wurtz" <da*******************@asdsoftware.com> wrote in message
news:3f********@news.splitrock.net... All,

Is there a way to force a derived class to contain an event. I have done
this with methods (i.e. Protected MustOverride Sub Test), but can I do
something similar with an event (Protected MustOverride Event MyEvent
doesn't work for obvious reasons)?

Thanks in advance.
Dave

Nov 20 '05 #4
Cheers for pointing out the giudelines. Guess I should have put a bit of
extra thought into the naming.
Making RaiseNameChanged private, how are derived classes suppose to call it? Or did I miss read you?
From the origional post, I took it that Dave wanted to force derived classes
to *handle* the event. I didn't think he wanted to raise them from there. I
tend to default to "less is more" when defining my methods (by making them
private unless I explicitly need to have them exposed), but obviously this
isn't the best option in all cases - especially when following the
guidelines on events.

Thanks again,

Trev.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl... Codemonkey,
Protected Mustoverride Sub OnNameChanged(e as EventArgs)
Not the best name for the MustOverride method, as OnNameChanged is

normally for the method to raise the event, per the Event Usage Guidelines in the
Design Guidelines for CLass Library Developers.
Private Sub RaiseNameChanged()
me.OnNameChanged(EventArgs.Empty)
raiseevent NameChanged(me, EventArgs.Empty)
End Sub Making RaiseNameChanged private, how are derived classes suppose to call

it? Or did I miss read you?

Don't get me wrong, I see where you are heading with this, and I like the
idea!

Hope this helps
Jay

"Codemonkey" <hu*********@hotmail.com> wrote in message
news:O3****************@TK2MSFTNGP12.phx.gbl...
Dave,

I don't think it is possible to force a derived class to override an

event.
You can of course make them override a protected method that is used in

the
base class to raise the event. E.g.

-------------------
*Base Class*

Public Event NameChanged(sender as Object, e as EventArgs)

Protected Mustoverride Sub OnNameChanged(e as EventArgs)

Private Sub RaiseNameChanged()
me.OnNameChanged(EventArgs.Empty)
raiseevent NameChanged(me, EventArgs.Empty)
End Sub

-------------------

When you want to raise the event, simply call RaiseNameChanged. This way,
you know that the derived class must handle OnNameChanged before the event is raised.

Hope this helps,

Trev.

"Dave Wurtz" <da*******************@asdsoftware.com> wrote in message
news:3f********@news.splitrock.net...
All,

Is there a way to force a derived class to contain an event. I have

done this with methods (i.e. Protected MustOverride Sub Test), but can I do
something similar with an event (Protected MustOverride Event MyEvent
doesn't work for obvious reasons)?

Thanks in advance.
Dave



Nov 20 '05 #5
Just out of interest, what would be the best way of making the derived class
override the method that raises the event, while still guaranteeing the
event is raised (if you use MustOverride, then you can't add code to the
method body to raise the event).

I dunno why you would ever want to do this, but just thought I'd ask anyway
(too much time on my hands ;)

Trev.

"Codemonkey" <hu*********@hotmail.com> wrote in message
news:ee**************@TK2MSFTNGP09.phx.gbl...
Cheers for pointing out the giudelines. Guess I should have put a bit of
extra thought into the naming.
Making RaiseNameChanged private, how are derived classes suppose to call it?
Or did I miss read you?


From the origional post, I took it that Dave wanted to force derived

classes to *handle* the event. I didn't think he wanted to raise them from there. I tend to default to "less is more" when defining my methods (by making them
private unless I explicitly need to have them exposed), but obviously this
isn't the best option in all cases - especially when following the
guidelines on events.

Thanks again,

Trev.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Codemonkey,
Protected Mustoverride Sub OnNameChanged(e as EventArgs)


Not the best name for the MustOverride method, as OnNameChanged is

normally
for the method to raise the event, per the Event Usage Guidelines in the
Design Guidelines for CLass Library Developers.
Private Sub RaiseNameChanged()
me.OnNameChanged(EventArgs.Empty)
raiseevent NameChanged(me, EventArgs.Empty)
End Sub

Making RaiseNameChanged private, how are derived classes suppose to call

it?
Or did I miss read you?

Don't get me wrong, I see where you are heading with this, and I like the
idea!

Hope this helps
Jay

"Codemonkey" <hu*********@hotmail.com> wrote in message
news:O3****************@TK2MSFTNGP12.phx.gbl...
Dave,

I don't think it is possible to force a derived class to override an

event.
You can of course make them override a protected method that is used in
the
base class to raise the event. E.g.

-------------------
*Base Class*

Public Event NameChanged(sender as Object, e as EventArgs)

Protected Mustoverride Sub OnNameChanged(e as EventArgs)

Private Sub RaiseNameChanged()
me.OnNameChanged(EventArgs.Empty)
raiseevent NameChanged(me, EventArgs.Empty)
End Sub

-------------------

When you want to raise the event, simply call RaiseNameChanged. This

way, you know that the derived class must handle OnNameChanged before the event is raised.

Hope this helps,

Trev.

"Dave Wurtz" <da*******************@asdsoftware.com> wrote in message
news:3f********@news.splitrock.net...
> All,
>
> Is there a way to force a derived class to contain an event. I have

done
> this with methods (i.e. Protected MustOverride Sub Test), but can I

do > something similar with an event (Protected MustOverride Event MyEvent > doesn't work for obvious reasons)?
>
> Thanks in advance.
> Dave
>
>



Nov 20 '05 #6
Codemonkey,
From the origional post, I took it that Dave wanted to force derived classes to *handle* the event. I didn't think he wanted to raise them from there. I
I'm not sure what Dave wanted. Hopefully between your & my post he provides
a little more info on what he is wanting.

Jay

"Codemonkey" <hu*********@hotmail.com> wrote in message
news:ee**************@TK2MSFTNGP09.phx.gbl... Cheers for pointing out the giudelines. Guess I should have put a bit of
extra thought into the naming.
Making RaiseNameChanged private, how are derived classes suppose to call it?
Or did I miss read you?


From the origional post, I took it that Dave wanted to force derived

classes to *handle* the event. I didn't think he wanted to raise them from there. I tend to default to "less is more" when defining my methods (by making them
private unless I explicitly need to have them exposed), but obviously this
isn't the best option in all cases - especially when following the
guidelines on events.

Thanks again,

Trev.

<<snip>>
Nov 20 '05 #7
Trev,
Unfortunately I don't really think you can. As the class that defines the
event has to raise the event. There is no code that you can put in a base
class per se that tells a derived class that it has to execute it.

The closest I can come up with is basically your example, which is not fool
proof ;-). The only real change I was proposing to your sample was to make
the names more consistent with the Guidelines.

Jay

"Codemonkey" <hu*********@hotmail.com> wrote in message
news:ex****************@TK2MSFTNGP10.phx.gbl...
Just out of interest, what would be the best way of making the derived class override the method that raises the event, while still guaranteeing the
event is raised (if you use MustOverride, then you can't add code to the
method body to raise the event).

I dunno why you would ever want to do this, but just thought I'd ask anyway (too much time on my hands ;)

Trev.

<<snip>>
Nov 20 '05 #8
> Unfortunately I don't really think you can.

I don't see why you would want to anyway. Best keeping things simple in my
opinion.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ux****************@TK2MSFTNGP10.phx.gbl...
Trev,
Unfortunately I don't really think you can. As the class that defines the
event has to raise the event. There is no code that you can put in a base
class per se that tells a derived class that it has to execute it.

The closest I can come up with is basically your example, which is not fool proof ;-). The only real change I was proposing to your sample was to make
the names more consistent with the Guidelines.

Jay

"Codemonkey" <hu*********@hotmail.com> wrote in message
news:ex****************@TK2MSFTNGP10.phx.gbl...
Just out of interest, what would be the best way of making the derived

class
override the method that raises the event, while still guaranteeing the
event is raised (if you use MustOverride, then you can't add code to the
method body to raise the event).

I dunno why you would ever want to do this, but just thought I'd ask

anyway
(too much time on my hands ;)

Trev.

<<snip>>

Nov 20 '05 #9

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

Similar topics

0
by: Andy Read | last post by:
Hello all, I have the requirement to produce source code that produces an object hierarchy. Example: Root | Folder 1
1
by: Eidolon | last post by:
I am looking to have a base class which provides that all inherited classes MUST have some property, but that they can each define it as whatever type they want. So maybe three class inherit from...
18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
2
by: Dave Wurtz | last post by:
All, Is there a way to force a derived class to contain an event. I have done this with methods (i.e. Protected MustOverride Sub Test), but can I do something similar with an event (Protected...
7
by: Iain Mcleod | last post by:
Hi This must be an often encountered problem. I want to declare an abstract class or an interface with nothing but several static constants so that I can use polymorphism when I call each of them...
7
by: Raterus | last post by:
I've got a good question about object-oriented programming, I hope I can explain it well enough. I'm creating a UserManager, which I'm going to inherit from so different applications can share...
41
by: JohnR | last post by:
In it's simplest form, assume that I have created a usercontrol, WSToolBarButton that contains a button. I would like to eventually create copies of WSToolBarButton dynamically at run time based...
9
by: jeff | last post by:
New VB user...developer... Situation...simplified... - I want to wrap a pre and post event around a system generated where the pre-event will always execute before the system event and the...
0
by: Altman | last post by:
I have created a class that has a mustoverride method. When I inherit this class VB automatically creates the overrides function for it. I was wondering if there was a way to have VB include a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.