Significance of calling the base

  • Thread starter Thread starter Frank Rizzo
  • Start date Start date
F

Frank Rizzo

In some examples with inheriting, I see the overloaded function always
call its base. For instance, in this case where the ListView control is
being extended.

public class ListViewEx : System.Windows.Forms.ListView
{
protected override void OnColumnClick(ColumnClickEventArgs e)
{
...various code to extend the control

base.OnColumnClick (e);
}
}

What is the significance of the base.OnColumnClick(e)? Is it needed or
is it superfluous? In some examples I see it, in others I don't. Is
there a rule for this?

Thanks.
 
it depends on whether you want the base class's OnColumnClick code to run or
not. Sometimes you'll want to completely replace what the base class does
(and you won't call the base class's method), and sometimes you want to
augment what it does (and you'd want to call the base method)
 
Frank Rizzo said:
In some examples with inheriting, I see the overloaded function always
call its base. For instance, in this case where the ListView control is
being extended.

public class ListViewEx : System.Windows.Forms.ListView
{
protected override void OnColumnClick(ColumnClickEventArgs e)
{
...various code to extend the control

base.OnColumnClick (e);
}
}

What is the significance of the base.OnColumnClick(e)? Is it needed or
is it superfluous? In some examples I see it, in others I don't. Is
there a rule for this?

Well, if you don't call the base implementation, then it obviously
won't do what it would normally do - such as fire the ColumnClick event
in this case. Sometimes you do want to call the base method, sometimes
you don't - but there's no general rule that I know of.
 
Jon said:
Well, if you don't call the base implementation, then it obviously
won't do what it would normally do - such as fire the ColumnClick event
in this case. Sometimes you do want to call the base method, sometimes
you don't - but there's no general rule that I know of.

Given that I don't have the source for the ListView control, how would I
know whether or not I should call the base for each specific case?
 
Frank Rizzo said:
Given that I don't have the source for the ListView control, how would I
know whether or not I should call the base for each specific case?

Well, do you want it to do what it would normally do or not? For the
OnXXX, it's really "do you want the normal events to fire" to which the
answer is usually yes, I'd expect.
 
Back
Top