C# Can a base class property be invoked from derived class

20,995

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
    }
}
Share:
20,995

Related videos on Youtube

Shahid
Author by

Shahid

Updated on July 09, 2022

Comments

  • Shahid
    Shahid almost 2 years

    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
            }
        }   
    }
    
    • Jakub Konecki
      Jakub Konecki over 13 years
      Have you actually tried to call base yourself?
    • Adam Crossland
      Adam Crossland over 13 years
      All your base are belong to you. Of course you can do this. You can use base inside a setter.
    • Vadim
      Vadim over 13 years
      can you provide an example of what you'd like to do?
    • Paolo Falabella
      Paolo Falabella over 13 years
      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"
    • Shahid
      Shahid over 13 years
      @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
    • Paolo Falabella
      Paolo Falabella over 13 years
      @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
      Paolo Falabella over 13 years
      @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
  • Shahid
    Shahid over 13 years
    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
  • Paolo Falabella
    Paolo Falabella over 13 years
    @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.
  • Shahid
    Shahid over 13 years
    Thanks paolo. I have been stupid to overlook this. I accepted your answer now
  • Paolo Falabella
    Paolo Falabella over 13 years
    @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