Search code examples
c#propertiesradixderived

Accessing properties of the base class in a double derived class


I am having trouble accessing the properties of the baseclass in a double derived class. I have included a simple example of my problem below.

public class Device
{
   public Device(string status)
   {
      Status = status;
   }

   public string Status { get; set; }

   public void SetStatus(string value)
   {
      Status = value;
   }

   public string GetStatus()
   {
      return Status;
   }
}

public class Light : Device
{
   public Light(string status) : base(status)
   {
   }

   public void SetStatus(string value)
   {
      base.SetStatus(value)
   }

   public string GetStatus()
   {
      return Status;
   }
}

public class ColoredLight : Light
{
   public ColoredLight(string status) : base(status)
   {
   }

   public void SetStatus(string value)
   {
      base.SetStatus(value)
   }

   public string GetStatus()
   {
      return Status;
   }
}

// note: The derived classes also have some properties of their own, of course, 
// but they are not needed to illustrate the problem

I know the Status property of the baseclass definitely gets set to a value, but if I call the GetStatus method in the ColoredLight class it returns null.

If I change the GetStatus functions in the Light and ColoredLight class to return base.GetStatus() it does return the correct value. However, I thought that it should be possible to access the properties of the baseclass directly in the derived classes. Am I doing something wrong? Or is this just how it should be done?

Thanks in advance for the help.

edit: [Solved] The problem I was having was due to stupidity and an oversight by myself, as I declared the Status property in both the Device and Light class. However, the solutions below have improved my understanding of the use of 'protected', 'virtual' and 'override' in C#, so I will leave the question as is, so it can maybe help others as well!


Solution

  • Get rid of the GetStatus() and SetStatus() methods altogether, this is not java. instead make the property virtual and override it in the descendant classes as needed.

    public class Device
    {
        public Device(string status)
        {
            this._status = status;
        }
    
        protected _status;
        public virtual string Status 
        { 
            get
            {
                return _status;
            }
            set
            {
                _status = value;
            }
        }
    }
    
    public class Light : Device
    {
        public Light(string status) : base(status)
        {
        }
    }
    
    public class ColoredLight : Light
    {
        public ColoredLight(string status) : base(status)
        {
        }
    
        public override string Status 
        { 
            get
            {
                return _status;
            }
            set
            {
                _status = value;
            }
        }
    }