tags:

views:

1058

answers:

2
class Child
{ 
    private override void Check_CheckedChanged(object sender, EventArgs e)
    {
        if (Check.Checked)
        {
            this.addName(this.FirstName);
            this.disableControls();
        }

        else
        {
            this.addAddress(this.address);
            //this.activatecontrols();// gives error since it's private method in parent.

        }
    }
}

class Parent
{
    private  void Check_CheckedChanged(object sender, EventArgs e)
    {
        if (Check.Checked)
        {
            this.disablecontrols();
        }

        else
        {
            this.addAddress(this.address);
            this.activatecontrols();
        }
    }

}

I want to fire the the child event if it satisfies if condition. But if can not I need to call the base's else condition as I activatecontrols() is private in Parent. So, how do I call the event?

+2  A: 

A very simple solution would be to make the ActivateControls protected virtual on Parent and override it on the child, then you can call base.activatecontrols in the child method if not Check.Checked.

Simon Wilson
Simon, add the code, and you'll get +1 :)
Sunny
I see Bryan already did. I was going to then my baby started bawling...getting him to sleep I thinking "someone's gonna vote me down 'cause of the vague answer". Also wanted the PO to think about it too.
Simon Wilson
There's no need to vote down for answer which leads to the right direction.
Sunny
+2  A: 

If you need the functionality of ActivateControls in the derived class, you can make it protected in the base class.

Alternately, you could have the Check_CheckedChanged event handler call a virtual method in the base class which can be overridden in the derived class:

// Parent.cs

private void Check_CheckedChanged(object sender, EventArgs e)
{
    OnCheckedChanged();
}

protected virtual void OnCheckedChanged()
{
    if (Check.Checked)
    {
        this.disablecontrols();
    }
    else
    {
        this.addAddress(this.address);
        this.activatecontrols();
    }
}

The logic for Parent does not need to be repeated in Child, so that handler can be simplified:

// Child.cs

protected override void OnCheckedChanged()
{
    if (Check.Checked)
    {
        this.addName(this.FirstName);
    }

    base.OnCheckedChanged();  // Same outcome
}
Bryan Watts
Wish I had noticed the private specifier on the handler. Never do that myself. Just assumed it was protected when I copied/pasted.
tvanfosson