Kullis Kullis - 3 months ago 12
C# Question

Stopping an override function from the base function

I have a base void function called Update() which has to determine if the override Update() should be processed. As I understand a simple return; in the base function only stops the base function and not the override. So is there a way of doing that without writing anything more than base.Update() in the override?

protected virtual void Update()
{
if (someArgument)
{
//Some code to stop the override from being processed
}
}

protected override void Update()
{
base.Update(); //It would be great if I didn't need to write anything more than this line to stop the override function
//Some other code
}

Answer

Well if you have another method that is virtual you can deside if the overrided method should be called. Something like this. But you can't from the base class stop the execution of overridden methods.

class BaseClass
{
    protected void Update()
    {
        if(someArgument)
        {
            this.InnerUpdate();
        }
    }

    protected virtual void InnerUpdate()
    {
    }
}

class Foo : BaseClass
{
    protected override void InnerUpdate()
    {
        //Some other code
    }
}