public get, protected set

How can I declare a property with a public get and a protected set accessor

Answer this question

public get, protected set

  • Shrek.NET

    What if the get is overriding from it's base class, but the set is just something new you want to expose.

    IE

    public abstract class BaseClass{public abstract string MyProp{get;}}

    public class ChildClass:BaseClass

    {

    public override string MyProp{

    get{ // DoSomething;}

    set{} // This is not part of the base class, so I get a compiler error.

    }

    }


  • kats

      public int Test
      {
         get
         {
            return test;
         }

         protected set
         {
            test = value;
         }
      }

     

    You may only mark one of the inner accessors with an additional access modifier.  And the modifier must be less accessible than the property access modifier.


  • Tony03

    It's just as easy as:

          public int SomeValue

          {

             get { return someValue; }

             protected set { someValue = value; }
          }

     Edit: ... provided you are using C# 2.0 as found in C# Express or Visual Studio 2005.



  • sureshv

    If you stuck with C# 1.0, then have the property just have a getter, and have the setter as a separate protected method.



  • mruniqueid

    Since what you really get from the base class is one virtual method called get_MyProp there will not be any setter to override which you try to do. Unfortunatly the compiler only makes that distinction for overrides, if you try to just add the setter and ignore the getter, it hides the original property get method, which is really silly :)

  • Joshizzle

    That really is silly. It should work something similar to changing the scope.

    public string Prop{

    override get{//}

    set{}

    }

    The compiled IL it's finally two different methods so I dont see a reason why this is not possible, well obviously the compiler, but I mean I dunno why is it that the compiler do allow that.

    Wjhat about VB or J# or Delphi, could a prop be overriden like that


  • BrunoAMSilva

    Thanks guys.

    I knew there was a reason to get VS2005....


  • public get, protected set