public get, protected set  
Author Message
search and deploy





PostPosted: Visual C# Language, public get, protected set Top

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

Visual C#8  
 
 
louthy





PostPosted: Visual C# Language, public get, protected set Top

  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.


 
 
Brendan Grant





PostPosted: Visual C# Language, public get, protected set Top

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.



 
 
James Curran





PostPosted: Visual C# Language, public get, protected set Top

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



 
 
theking2





PostPosted: Visual C# Language, public get, protected set Top

Thanks guys.

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


 
 
mamadero





PostPosted: Visual C# Language, public get, protected set Top

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.

}

}


 
 
Lowendahl





PostPosted: Visual C# Language, public get, protected set Top

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 :)

 
 
mamadero





PostPosted: Visual C# Language, public get, protected set Top

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