By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,486 Members | 1,024 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,486 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.