Call to Base-Class Does Not Succeed

M

Martijn Mulder

Documentation suggests that I should make a call to the base-class method
for some methods. When I try to do so for the class below, I get wrong
results. What is the correct way to make a call to the base class for an
event like this?


//class Form
class Form:System.Windows.Forms.Form
{

//constructor
Form()
{
MouseEnter+=OnMouseEnter;
}


//OnMouseEnter
void OnMouseEnter(System.Object a,System.EventArgs b)
{
base.OnMouseEnter(b);
}


//Main
[System.STAThread]
static void Main()
{
System.Windows.Forms.Application.Run(new Form());
}
}
 
T

Tom Porterfield

Martijn said:
Documentation suggests that I should make a call to the base-class method
for some methods. When I try to do so for the class below, I get wrong
results. What is the correct way to make a call to the base class for an
event like this?


//class Form
class Form:System.Windows.Forms.Form
{

//constructor
Form()
{
MouseEnter+=OnMouseEnter;
}


//OnMouseEnter
void OnMouseEnter(System.Object a,System.EventArgs b)
{
base.OnMouseEnter(b);
}


//Main
[System.STAThread]
static void Main()
{
System.Windows.Forms.Application.Run(new Form());
}
}

If you are handling this throug events as you are, then there is no need to
call the base class as that has already happened by the time you get to your
event handler. If instead of using event handlers you had overriden the
OnMouseEnter method of the base class, then in your override you would want
to make sure you called the base. That would like similar to as follows:

//class Form
class Form:System.Windows.Forms.Form
{

//constructor
Form()
{
}

//OnMouseEnter
protected override void OnMouseEnter(System.EventArgs b)
{
base.OnMouseEnter(b);
}

//Main
[System.STAThread]
static void Main()
{
System.Windows.Forms.Application.Run(new Form());
}
}
 

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