473,406 Members | 2,259 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,406 software developers and data experts.

Overriding methods but force base class's method to be called.

I have a question about overriding i was wondering if anyone could
help me with, or even suggesting a better/different way. I have no
idea if this can even be done or not.
I was wondering if there was anyway to force a class to call a base
class's method that it is overriding? Almost the same way you have to
call a base class's constructor if it has arguments.
(example ** assuming the Person class's constructor has (string
FirstName) as its arguments.
Employee Class will inherit from Person. I know there are different
ways of instantiating classes, but this was just a simple way for me
to explain it.
the Class would be defined as:
public class Employee(string FirstName) : Person : base
(FirstName) ......
in the above example you have to call the : base (FirstName), since
the Person's class has that signature for its constructor.

Basically, I have classes that all have a Validate method. For classes
that inherit from other classes, They have their own Validate method
( which they override the base class's Validate method), but i want to
force them to also call base.Validate(); to force the base class's
validate method to be called as well.
Example, if i have a Person class, my Validate() method might force a
First and LastName, a valid SSN and a valid birthdate. If I have an
Employee class, its Validate method might check for a valid hire date,
a valid department and a valid salary. When the Employee.Validate is
called, i want to force this class to also call the base.Validate
( which is the person class ). This way a developer can't forget to
call the base.Validate and make a mistake.

Does anyone know how to do this, or suggest a better / different way
of making this happen.
Thanks
Bryan

Feb 6 '07 #1
6 27728

One mechanism is to declare two methods. Say you have a method
Update() and you want anyone that override Update to have to call your
base class implementation. Then don't make Update virtual but instead
declare a virtual UpdateEx method and call that from the start of
Update(). Children can override UpdateEx and you can guarantee that
Update will still be called:

public void Update() {
UpdateEx();
...
}

protected virtual void UpdateEx() {
// for extension
}

You can also declare BeforeUpdate, AfterUpdate or whatever suits your
needs.

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On 6 Feb 2007 10:43:08 -0800, br*********@yahoo.com wrote:
>I have a question about overriding i was wondering if anyone could
help me with, or even suggesting a better/different way. I have no
idea if this can even be done or not.
I was wondering if there was anyway to force a class to call a base
class's method that it is overriding? Almost the same way you have to
call a base class's constructor if it has arguments.
(example ** assuming the Person class's constructor has (string
FirstName) as its arguments.
Employee Class will inherit from Person. I know there are different
ways of instantiating classes, but this was just a simple way for me
to explain it.
the Class would be defined as:
public class Employee(string FirstName) : Person : base
(FirstName) ......
in the above example you have to call the : base (FirstName), since
the Person's class has that signature for its constructor.

Basically, I have classes that all have a Validate method. For classes
that inherit from other classes, They have their own Validate method
( which they override the base class's Validate method), but i want to
force them to also call base.Validate(); to force the base class's
validate method to be called as well.
Example, if i have a Person class, my Validate() method might force a
First and LastName, a valid SSN and a valid birthdate. If I have an
Employee class, its Validate method might check for a valid hire date,
a valid department and a valid salary. When the Employee.Validate is
called, i want to force this class to also call the base.Validate
( which is the person class ). This way a developer can't forget to
call the base.Validate and make a mistake.

Does anyone know how to do this, or suggest a better / different way
of making this happen.
Thanks
Bryan
Feb 6 '07 #2
On Tue, 06 Feb 2007 10:43:08 -0800, <br*********@yahoo.comwrote:
I have a question about overriding i was wondering if anyone could
help me with, or even suggesting a better/different way. I have no
idea if this can even be done or not.
I was wondering if there was anyway to force a class to call a base
class's method that it is overriding?
I may be misunderstanding your question. But if not, then you should be
able to simply do something like this:

public override void Validate()
{
base.Validate();
// override code goes here
}

If for some reason you want the base class stuff to be executed after your
own stuff, just put the base.Validate() line later, wherever is
appropriate.

Pete
Feb 6 '07 #3
On Feb 6, 3:12 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Tue, 06 Feb 2007 10:43:08 -0800, <bryanbab...@yahoo.comwrote:
I have a question aboutoverridingi was wondering if anyone could
help me with, or even suggesting a better/different way. I have no
idea if this can even be done or not.
I was wondering if there was anyway toforcea class to call a base
class's method that it isoverriding?

I may be misunderstanding your question. Butif not, then you should be
able to simply do something like this:

public override void Validate()
{
base.Validate();
// override code goes here
}

If for some reason you want the base class stuff to be executed after your
own stuff, just put the base.Validate() line later, wherever is
appropriate.

Pete
Hey Peter, thanks for the reply.
the base.Validate(); will call the base method, but it doesn't prevent
someone that inherits from the class to be forced to call
base.Validate(), i want to force anyone inhertiting from my that
overrides Validate to have to call base.Validate or their class won't
compile.
Thanks for taking the time to respond.
Bryan

Feb 8 '07 #4
On Feb 6, 2:32 pm, Samuel R. Neff <samueln...@nomail.comwrote:
One mechanism is to declare twomethods. Say you have a method
Update() and you want anyone that override Update to have to call your
base class implementation. Then don't make Update virtualbutinstead
declare a virtual UpdateEx method and call that from the start of
Update(). Children can override UpdateEx and you can guarantee that
Update will still be called:

public void Update() {
UpdateEx();
...

}

protected virtual void UpdateEx() {
// for extension

}

You can also declare BeforeUpdate, AfterUpdate or whatever suits your
needs.

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On 6 Feb 2007 10:43:08 -0800, bryanbab...@yahoo.com wrote:
I have a question aboutoverridingi was wondering if anyone could
help me with, or even suggesting a better/different way. I have no
idea if this can even be done or not.
I was wondering if there was anyway toforcea class to call a base
class's method that it isoverriding? Almost the same way you have to
call a base class's constructor if it has arguments.
(example ** assuming the Person class's constructor has (string
FirstName) as its arguments.
Employee Class will inherit from Person. I know there are different
ways of instantiating classes,butthis was just a simple way for me
to explain it.
the Class would be defined as:
public class Employee(string FirstName) : Person : base
(FirstName) ......
in the above example you have to call the : base (FirstName), since
the Person's class has that signature for its constructor.
Basically, I have classes that all have a Validate method. For classes
that inherit from other classes, They have their own Validate method
( which they override the base class's Validate method),buti want to
forcethem to also call base.Validate(); toforcethe base class's
validate method to be called as well.
Example, if i have a Person class, my Validate() method mightforcea
First and LastName, a valid SSN and a valid birthdate. If I have an
Employee class, its Validate method might check for a valid hire date,
a valid department and a valid salary. When the Employee.Validate is
called, i want toforcethis class to also call the base.Validate
( which is the person class ). This way a developer can't forget to
call the base.Validate and make a mistake.
Does anyone know how to do this, or suggest a better / different way
of making this happen.
Thanks
Bryan- Hide quoted text -

- Show quoted text -
Very good idea, one thing i thought of though as i was thinking about
this. they can still just call UpdateEx from theirs, since they never
called the regular Update method, this never fires to also call the
main class's UpdateEx method.
This is cool though, i have to think about this. This actually solves
my problem with another thing. I have a form that i use as my master
form, which i have an event for Application.Close. I wanted forms to
be able to override this but to also have to call the Parent Close
method. I can create a method in my class that gets the
Application.Close event, then calls ApplicationCloseEx which can be
overriden, which i can always make sure i run my ApplicationClose
etc....
Basically you can almost do something using Template method design
pattern I guess.
NICE !!!
thanks again.

Feb 8 '07 #5
On 8 Feb 2007 11:29:10 -0800, br*********@yahoo.com wrote:

>Very good idea, one thing i thought of though as i was thinking about
this. they can still just call UpdateEx from theirs, since they never
called the regular Update method, this never fires to also call the
main class's UpdateEx method.
Yeah, that's why UpdateEx is declared protected.. but it doesn't stop
the child class from calling UpdateEx directly. Sometimes it has to
fall on coding standards and documentation and code reviews.

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
Feb 8 '07 #6
On Thu, 08 Feb 2007 11:22:25 -0800, <br*********@yahoo.comwrote:
[...] i want to force anyone inhertiting from my that
overrides Validate to have to call base.Validate or their class won't
compile.
I see. Then yes, I misunderstood your question. And I guess I'm left
just agreeing with what Samuel wrote. :)

I'm not aware of any 100% effective language support for what you're
trying to do, so at some point you're just going to have to rely on
trusting the derived class to do the right thing.

Though, now that I think about it, I suppose that one thing you could do
is use reflection somehow to inspect the derived versions of the calls and
throw an exception if they don't include a call to the base version. This
doesn't address your request to prevent the code from compiling, but if
you did it when the module loaded, that'd be almost as good.

Unfortunately, I don't know enough about reflection to tell you how to do
this, or even to guarantee that it's possible. It just seems like it
probably is. :)

Pete
Feb 9 '07 #7

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

Similar topics

6
by: Robert | last post by:
Hello all... In my code below, the Notify Constructor and Destructor is getting called twice and it appears that a new Notify object is created on the 2nd call. The 2nd call is caused by this...
5
by: Luis G Hernandez | last post by:
I am required to create a pure virtual method for a project that returns a string. The method name is: getCoverType() I know that for a data type int I could do this: protected: // method to...
1
by: Tariq | last post by:
Any thoughts on how a method on a base class can be prevented from being overriden or hidden? Basically the idea is that no matter what all derived classes should use the method in my base...
4
by: yaron | last post by:
Hi, how can i prevent from a derive class to override/hide a base class method ? because in c# there is the new keyword on a method. i am looking for the same functionality as the 'final'...
2
by: Raider | last post by:
I have class hierarchy with virtual methods like this: class Base { public: virtual void Show(); ~Base(); }; class Derived : public Base
2
by: Kevin Frey | last post by:
In a derived class I am trying to redefine the implementation of a interface method defined in a base class - the base class interface method was not declared virtual. I have yet to actually...
11
by: Aflj | last post by:
This code won't compile (two compilers tried, gcc and VC++, both of recent versions, but I don't remember them exactly): class C1 { public: void M1(int i) {} }; class C2: public C1
3
by: rajanipro | last post by:
Hi buddies! Can you tell me how to invoke base class static method hidden by inheritance, using derived class as in the following case? using System; class ParentClass { public static...
0
by: ma740988 | last post by:
Consider # include <iostream> # include <vector> # include <typeinfo> # include <string> class BaseMsg { friend std::ostream& operator<<( std::ostream&, const BaseMsg& ); std::string name...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.