Sw3das
Sw3das

Reputation: 157

C# - Overriding only part of the function

I have a class, called Party and function in it, which is called OnMemberDeath(). I've created new class called PlayerParty which inherits from Party and now I need to add only two but very important lines to the OnMemberDeath( ) function ( Example below ). Is it possible to override only part of the method?

What I'm trying to achieve

Party class is responsible for detecting turn-based combat stuff. Also, every Party has members ( entities ) and SelectionOrb ( little circle below them ). When member of a party dies, new member has to be selected. Through the member selection process the SelectionOrb has to receive new parent ( AdjustWorldObject() ) as well as the reference to selected member has to be assigned ( for battle stuff ). If none alive members are left, we need to set parent to null so that SelectionOrb is not deleted together with the Member object.
In PlayerParty class there's new object, which is CombatUI and it is acting basicly the same as SelectionOrb( has to receive new parent after current's death ). Hopefully this is clear enough.

    /** in Party class **/
protected void OnMemberDeath( )
{
    if( aliveCount > 1 )
    {
        AdjustWorldObject( );
        aliveCount--;
    }

    else
    {
        ResetWorldObject( );

        if( OnPartyDeath != null ) OnPartyDeath( ); // Event
        else print( "Party died but event had no subscriptions!" );
    }
}

    /** in PlayerParty class **/
new void OnMemberDeath( )
{
    if( aliveCount > 1 )
    {
        AdjustWorldObject( );
        AdjustCombatUI( ); // The first line I want to add
    }

    else
    {
        ResetWorldObject( );
        ResetCombatUI( ); // The second line I want to add

        if( OnPartyDeath != null ) OnPartyDeath( ); // Event
        else print( "Party died but event had no subscriptions!" );
    }
}

Upvotes: 0

Views: 2304

Answers (1)

Ivan Stoev
Ivan Stoev

Reputation: 205889

Is it possible to override only part of the method ?

No, it's not possible. You need to extract the parts of the function that needs to be overridden into a new virtual methods.

For instance, if your AdjustWorldObject and ResetWorldObject are called only from this method, you can make them virtual and override when needed like this

class Party
{
    protected void OnMemberDeath( )
    {
        if(aliveCount > 1)
        {
            AdjustWorldObject();
            aliveCount--;
        }
        else
        {
            ResetWorldObject();
            if(OnPartyDeath != null) OnPartyDeath(); // Event
            else print( "Party died but event had no subscriptions!" );
        }
    }
    protected virtual void AdjustWorldObject()
    {
        // ...
    }
    protected virtual void ResetWorldObject()
    {
        // ...
    }
}

class PlayerParty : Party
{
    protected override void AdjustWorldObject()
    {
        base.AdjustWorldObject();
        AdjustCombatUI();
    }
    protected override void ResetWorldObject()
    {
        base.ResetWorldObject();
        ResetCombatUI();
    }
}

If they are not, you can create similar dedicated virtual methods.

Upvotes: 5

Related Questions