Novice question about overridable subs

  • Thread starter Thread starter Ronald S. Cook
  • Start date Start date
R

Ronald S. Cook

If I have the following in a master user control...

Protected Overridable Sub CaptureFields()
DoThis...
End Sub

And then this in an inherited user control...

Protected Overrides Sub CaptureFields()
DoThat...
End Sub

What happens?

Will "DoThis" execute and then "DoThat"? Will "DoThis" never execute? How
does it work (i.e. how do they work together if they do?)

Thanks for any response.
 
Override means it is over ridden (ie, none of the base functionality exists,
from the standpoint of the inheriting class). If you want to call the base
implementation, you will have to explicitly call it (ie, if you want to do
this, you will have to wire the overridign method to the one it overrides).

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************
| Think outside the box!
|
*************************************************
 
If I have the following in a master user control...

protected virtual void CaptureFields()
{
// do this...
}

And then this in an inherited user control...

protected override void CaptureFields()
{
// do that...
}

(I fixed your syntax errors for you...the code you posted wasn't valid C#
code).
Will "DoThis" execute and then "DoThat"? Will "DoThis" never execute?
How does it work (i.e. how do they work together if they do?)

As Gregory said, for an instance of the inheriting class, calling
CaptureFields() only executes the code in the override.

You can have that override method inherit the parent class behavior too,
by calling the base method:

protected override void CaptureFields()
{
base.CaptureFields();
// do that...
}

You can call the base method anywhere in the override you want. So if the
order of the operations is important, you have control over that.

Pete
 
Thanks guys. Each implementation of the sub will need to change the cursor
to the "waiting" mode and then back to default at the end. I was hoping to
somehow put this in one place in the master but it looks like each developer
will just need to make sure they end-cap the sub with those lines each time.

Thanks again,
Ron
 
Thanks guys. Each implementation of the sub will need to change the
cursor to the "waiting" mode and then back to default at the end. I was
hoping to somehow put this in one place in the master but it looks like
each developer will just need to make sure they end-cap the sub with
those lines each time.

Well, you could wrap the call to the virtual method with a base class
method that handles the cursor changes:

base class:

protected void CaptureFieldsWithWait()
{
// set cursor
CaptureFields();
// restore cursor
}

protected override void CaptureFields()
{
// do this
}

subclass:

protected override void CaptureFields()
{
// do that
}

I'm curious as to what behavior you thought _might_ have occurred that
would have avoided some specific explicit handling of the cursor change.
If you can describe succinctly the actual flow you are thinking of that
might do what you need, it's possible other suggestions might be
forthcoming as to how to best implement that.

Pete
 
Hi,

First of all, that is VB code, this is a C# NG, so I will assume you will
work in C#.

if the instance you have is of type Inehrited will execute the overridden
method ("DO THAT").
If you want you can call the parent using base.CaptureFields()
 
This was great help.. thanks.

Peter Duniho said:
Well, you could wrap the call to the virtual method with a base class
method that handles the cursor changes:

base class:

protected void CaptureFieldsWithWait()
{
// set cursor
CaptureFields();
// restore cursor
}

protected override void CaptureFields()
{
// do this
}

subclass:

protected override void CaptureFields()
{
// do that
}

I'm curious as to what behavior you thought _might_ have occurred that
would have avoided some specific explicit handling of the cursor change.
If you can describe succinctly the actual flow you are thinking of that
might do what you need, it's possible other suggestions might be
forthcoming as to how to best implement that.

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

Back
Top