views:

366

answers:

3

I'm trying to create a set of classes where a common ancestor is responsible for all the logic involved in setting various properties, and the descendants just change the access of properties depending on whether they are required in the particular descendant.

When I try to do it as shown below I get a compiler error: "cannot change access modifiers when overriding 'protected' inherited member"

Is there a way to achieve what I'm trying to do? Thanks

public class Parent
{
     private int _propertyOne;
     private int _propertyTwo;

     protected virtual int PropertyOne
     {
          get { return _propertyOne; }
          set { _propertyOne = value; }
     }

     protected virtual int PropertyTwo
     {
          get { return _propertyTwo; }
          set { _propertyTwo = value; }
     }
}

public class ChildOne : Parent
{
    public override int PropertyOne  // Compiler Error CS0507
    {
        get { return base.PropertyOne; }
        set { base.PropertyOne = value; }
    }
    // PropertyTwo is not available to users of ChildOne
}

public class ChildTwo : Parent
{
    // PropertyOne is not available to users of ChildTwo
    public override int PropertyTwo  // Compiler Error CS0507
    {
        get { return base.PropertyTwo; }
        set { base.PropertyTwo = value; }
    }
}
+4  A: 

You can do this by using "new" instead of "override" to hide the parent's protected property as follows:

public class ChildOne : Parent
{
    public new int PropertyOne  // No Compiler Error
    {
        get { return base.PropertyOne; }
        set { base.PropertyOne = value; }
    }
    // PropertyTwo is not available to users of ChildOne
}

public class ChildTwo : Parent
{
    // PropertyOne is not available to users of ChildTwo
    public new int PropertyTwo
    {
        get { return base.PropertyTwo; }
        set { base.PropertyTwo = value; }
    }
}
Jose Basilio
OOPS .. Seconds faster :) .. Anyways using new is not the same as override; new hides the parent member and this way it's no longer Polymorphism.
Galilyou
@José, thanks that does what I need.
WileCau
+2  A: 

NO. Still you can Hide the inherited property with Your's

public class ChildTwo: Praent { 
    public new int PropertyTwo { 
       // do whatever you want
    }
}

ps: this is no longer virtual/override relationship (i.e. no polymorphic calls)

Galilyou
@7alwagy, thanks. In my case the virtual/override doesn't matter, I just thought I needed it to override the base property. Using 'new' instead of 'override' does the trick.
WileCau
+4  A: 

You can't change the access, but you can re-declare the member with greater access:

public new int PropertyOne
{
    get { return base.PropertyOne; }
    set { base.PropertyOne = value; }
}

The problem is that this is a different PropertyOne, and inheritance / virtual might not work as expected. In the above case (where we just call base.*, and the new method isn't virtual) that is probably fine. If you need real polymorphism above this, then you can't do it (AFAIK) without introducing an intermediate class (since you can't new and override the same member in the same type):

public abstract class ChildOneAnnoying : Parent {
    protected virtual int PropertyOneImpl {
        get { return base.PropertyOne; }
        set { base.PropertyOne = value; }
    }
    protected override int PropertyOne {
        get { return PropertyOneImpl; }
        set { PropertyOneImpl = value; }
    }
}
public class ChildOne : ChildOneAnnoying {
    public new int PropertyOne {
        get { return PropertyOneImpl; }
        set { PropertyOneImpl = value; }
    }
}

The important point in the above is that there is still a single virtual member to override: PropertyOneImpl.

Marc Gravell
@Marc, I probably confused things because I thought I needed the virtual/override but apparently in this case I don't. I'll keep a link to your answer for when I do need to do it with polymorphism :) Thanks for the explanation and sample code.
WileCau