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

B

bryanbabula

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
 
S

Samuel R. Neff

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
 
P

Peter Duniho

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
 
B

bryanbabula

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
 
B

bryanbabula

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.





- 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.
 
S

Samuel R. Neff

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
 
P

Peter Duniho

[...] 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top