24

I have a base class with a property which has a setter method. Is there a way to invoke the setter in the base class from a derived class and add some more functionality to it just like we do with overriden methods using the base keyword.

Sorry I should have added an example. Here is an example. Hope I get it right:

public class A 
{
    public abstract void AProperty 
    {
        set 
        {
            // doing something here
        }
    }
}

public class B : A 
{   
    public override void AProperty 
    {
        set 
        {
            // how to invoke the base class setter here

            // then add some more stuff here
        }
    }   
}
Shahid
  • 995
  • 3
  • 11
  • 24
  • Have you actually tried to call base yourself? – Jakub Konecki Feb 24 '11 at 17:37
  • 4
    All your base are belong to you. Of course you can do this. You can use base inside a setter. – Adam Crossland Feb 24 '11 at 17:39
  • can you provide an example of what you'd like to do? – Vadim Feb 24 '11 at 17:39
  • 1
    your setter in the base class should be virtual instead of abstract if you want to have code in place of your comment "doing something here" – Paolo Falabella Feb 24 '11 at 18:07
  • @paolo ok lets say I have it as virtual. my question still remains how do I invoke that code with a single call rather than having to re-write it in the derived setter or putting it in another protected method and calling it – Shahid Feb 24 '11 at 18:09
  • @Shahid maybe I'm being dense and I don't understand your objection... When you assign to base.t in the derived class you are actually using the setter in the base class, so if you have more logic inside the base setter it will indeed be invoked – Paolo Falabella Feb 24 '11 at 18:18
  • @Shahid I changed my example a little to show that indeed the base setter is called when you assign a value to the inherited property – Paolo Falabella Feb 24 '11 at 19:11

1 Answers1

39

EDIT: the revised example should demostrate the order of invocations. Compile as a console application.

class baseTest 
{
    private string _t = string.Empty;
    public virtual string t {
        get{return _t;}
        set
        {
            Console.WriteLine("I'm in base");
            _t=value;
        }
    }
}

class derived : baseTest
{
    public override string t {
        get { return base.t; }
        set 
        {
            Console.WriteLine("I'm in derived");
            base.t = value;  // this assignment is invoking the base setter
        }
    }
}

class Program
{

    public static void Main(string[] args)
    {
        var tst2 = new derived();
        tst2.t ="d"; 
        // OUTPUT:
        // I'm in derived
        // I'm in base
    }
}
Paolo Falabella
  • 24,914
  • 3
  • 72
  • 86
  • You are not actually invoking what the setter in the base class property 't' is doing. If the base setter 't' had 10 lines of code, can we call it from the derived class so those 10 lines of code are executed and then we add additional lines of code – Shahid Feb 24 '11 at 18:07
  • @Shahid yes I am. Try changing the code in the base class setter to set { Console.WriteLine("I'm in base"); _t=value;} and see for yourself. – Paolo Falabella Feb 24 '11 at 18:10
  • Thanks paolo. I have been stupid to overlook this. I accepted your answer now – Shahid Feb 25 '11 at 10:28
  • @Shahid no worries, I'm not a native english speaker and I really thought I wasn't understanding what you needed to do... Glad I've been of help – Paolo Falabella Feb 25 '11 at 10:42