473,383 Members | 1,859 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,383 software developers and data experts.

enforce event calling on inherited classes

I have an abstract class that about a dozen sub-classes inherit from. I
want to enforce that each sub-class shadows an event in the abstract class,
but can't quite figure out how to do this.

Basically, all of the inherited classes deal with data in some way or
another. I need to make sure that when the data is changed on any of those
classes, they raise an event called "DataChanged", and have that enforced in
some way from the abstract class.

I've tried a number of approaches, including "MustOverride Event", which
apparently isn't allowed. Also, I've created an interface with just the
DataChanged event, but there doesn't appear to be any sort of "Must
Implement" available --- to make matters worse, when trying to raise an
event from the inherited class on the abstract class, that doesn't appear to
be allowed either.

Obviously I can create an interface and just "remember" that every one of
the sub-classes must implement this interface, but that's error prone and
relies on me remembering additional steps, which obviously isn't a good
idea.

Any suggestions?
Jul 16 '06 #1
3 1991
Hi Matt,
One possibility. Have the base class event handler delegate (call) a
mustoverride sub.
--
Terry
"Matt F." wrote:
I have an abstract class that about a dozen sub-classes inherit from. I
want to enforce that each sub-class shadows an event in the abstract class,
but can't quite figure out how to do this.

Basically, all of the inherited classes deal with data in some way or
another. I need to make sure that when the data is changed on any of those
classes, they raise an event called "DataChanged", and have that enforced in
some way from the abstract class.

I've tried a number of approaches, including "MustOverride Event", which
apparently isn't allowed. Also, I've created an interface with just the
DataChanged event, but there doesn't appear to be any sort of "Must
Implement" available --- to make matters worse, when trying to raise an
event from the inherited class on the abstract class, that doesn't appear to
be allowed either.

Obviously I can create an interface and just "remember" that every one of
the sub-classes must implement this interface, but that's error prone and
relies on me remembering additional steps, which obviously isn't a good
idea.

Any suggestions?
Jul 16 '06 #2
Hello Matt F.,

It's quite simple really. I chose to model a predatory animal for my example..
but the principles are there for your data manipulation classes.
Define your event in an interface:

Public Interface IPredator

Event Eatten()

End Interface

Then create an abstract base class from which all others will inherit, which
implements the interface:

Public MustInherit Class Predator
Implements IPredator

Public Event Eatten(ByVal tSender As Object, ByVal tEatten As Object) Implements
IPredator.Eatten
Public Overridable Sub Eat()

RaiseEvent Eatten()

End Sub

End Class

Notice the Eat() sub. It's sole job is to raise the Eatten event on our
abstract class.

Now create a subclass of our base class:

Public Class Tiger
Inherits Predator

Public Overrides Sub Eat()

MsgBox("Yummie")
MyBase.Eat()

End Sub

End Class

Here you see that the Eat() sub overrides the base implementation. The last
thing we do here is call the base class's Eat() method, which will raise
the Eatten event.

The eatten event is available on the subclass like so:

Dim tTiger as Tiger = New Tiger
AddHandler tTiger.Eatten, AddressOf some_event_handler
Enjoy.

-Boo

I have an abstract class that about a dozen sub-classes inherit from.
I want to enforce that each sub-class shadows an event in the abstract
class, but can't quite figure out how to do this.

Basically, all of the inherited classes deal with data in some way or
another. I need to make sure that when the data is changed on any of
those classes, they raise an event called "DataChanged", and have that
enforced in some way from the abstract class.

I've tried a number of approaches, including "MustOverride Event",
which apparently isn't allowed. Also, I've created an interface with
just the DataChanged event, but there doesn't appear to be any sort of
"Must Implement" available --- to make matters worse, when trying to
raise an event from the inherited class on the abstract class, that
doesn't appear to be allowed either.

Obviously I can create an interface and just "remember" that every one
of the sub-classes must implement this interface, but that's error
prone and relies on me remembering additional steps, which obviously
isn't a good idea.

Any suggestions?

Jul 16 '06 #3
Thank you for such a well written answer and fully qualified code. It gets
me 95% of the way there, and now I just need to make sure that I do call the
DataChanged event at the property time.

"GhostInAK" <gh*******@gmail.comwrote in message
news:c7**************************@news.microsoft.c om...
Hello Matt F.,

It's quite simple really. I chose to model a predatory animal for my
example.. but the principles are there for your data manipulation classes.
Define your event in an interface:

Public Interface IPredator

Event Eatten()

End Interface

Then create an abstract base class from which all others will inherit,
which implements the interface:

Public MustInherit Class Predator
Implements IPredator

Public Event Eatten(ByVal tSender As Object, ByVal tEatten As Object)
Implements IPredator.Eatten
Public Overridable Sub Eat()

RaiseEvent Eatten()

End Sub

End Class

Notice the Eat() sub. It's sole job is to raise the Eatten event on our
abstract class.

Now create a subclass of our base class:

Public Class Tiger
Inherits Predator

Public Overrides Sub Eat()

MsgBox("Yummie")
MyBase.Eat()

End Sub

End Class

Here you see that the Eat() sub overrides the base implementation. The
last thing we do here is call the base class's Eat() method, which will
raise the Eatten event.

The eatten event is available on the subclass like so:

Dim tTiger as Tiger = New Tiger
AddHandler tTiger.Eatten, AddressOf some_event_handler
Enjoy.

-Boo

>I have an abstract class that about a dozen sub-classes inherit from.
I want to enforce that each sub-class shadows an event in the abstract
class, but can't quite figure out how to do this.

Basically, all of the inherited classes deal with data in some way or
another. I need to make sure that when the data is changed on any of
those classes, they raise an event called "DataChanged", and have that
enforced in some way from the abstract class.

I've tried a number of approaches, including "MustOverride Event",
which apparently isn't allowed. Also, I've created an interface with
just the DataChanged event, but there doesn't appear to be any sort of
"Must Implement" available --- to make matters worse, when trying to
raise an event from the inherited class on the abstract class, that
doesn't appear to be allowed either.

Obviously I can create an interface and just "remember" that every one
of the sub-classes must implement this interface, but that's error
prone and relies on me remembering additional steps, which obviously
isn't a good idea.

Any suggestions?


Jul 16 '06 #4

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

Similar topics

18
by: bhavin | last post by:
Hi, Can someone point me to some good best practices kind of documentation on use of events compared to method overriding. Ex. In Windows Forms when should i have an event handler for Paint, and...
1
by: WFB | last post by:
Hi, I have a base class from which all of my pages derive (ABCBasePage). For example, ABCCustomerSelect Inherits ABCPasePage. I would now like to have ABCPocketSelect which should inherit from...
6
by: crk2 | last post by:
Here a simple one. (At least I think it is?) and any help would be truly appreciated. I have an inherited textbox on my form based on a custom texbox control. It looks something like this ...
12
by: torbs | last post by:
Hi I have a a function with several methods. For simplicity it looks a bit like this: super.prototype.aProperty="HELLO"; super.prototype.returnValue = function () { return 2;
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...
8
by: Mike C# | last post by:
Suppose I have a base class "foo". Another class, "bar" derives from it. Base class "foo" has a method called "rob_the_liquor_store()", and the inherited class "bar" overrides this method with one...
14
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming...
12
by: Janaka Perera | last post by:
Hi All, We have done a object oriented design for a system which will create a class multiply inherited by around 1000 small and medium sized classes. I would be greatful if you can help me...
15
by: Barry Flynn | last post by:
VB 2005. I have the following code in a Sub. Dim oFred As SillyClass oFred = New SillyClass oFred.Gloop() oFred = Nothing Exit Sub
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.