views:

745

answers:

3

If I have a property that I want to let inheritors write to, but keep readonly externally, what is the preferred way to implement this? I usually go with something like this:

private object m_myProp;
public object MyProp
{
    get { return m_myProp; }
}
protected void SetMyProp(object value)
{
    m_myProp = value;
}

Is there a better way?

+33  A: 
private object m_myProp;
public object MyProp
{
    get { return m_myProp; }
    protected set { m_myProp = value; }
}

Or in C# 3.0

public object MyProp {get; protected set;}
Marc Gravell
A: 

Having a setter and getter isn't really any better than having a variable at that level of visibility.

Therefore you could just make the variable itself protected and the reader public.

That said, setters and getters are an indicator of bad OO--are you sure you need them? You should be asking the object to do something with its members, not asking it for its members then manipulating them outside the object.

This is a very general rule and there are a lot of exceptions.

Bill K
Nonsense; a getter/setter is *significantly* better than a protected field. Getters/setters are *not* an indicator of bad OO: they exhibit encapsulation and allow polymorphism (via virtual) - how OO do you want?
Marc Gravell
They still expose the implementation of your class, and are an indication that you are getting data out instead of asking an class to do an operation for you. That's not OO, it's fake-oo that procedural programmers use before they actually get OO.
Bill K
Here's a good article about it if you are interested: http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html
Bill K
I'm not quite prepared to say that article's wrong in every particular, but it's wrong in every particular that I examined.
Robert Rossney
@Bill K: no; the accessors (get/set) are methods; the caller calls the methods to ask the class to do something... I'd agree with Robert: that article is a bit, erm, wrong. If you want an immutable class, then fine - but you need to be able to examine state.
Marc Gravell
Another way to phrase the principle here, from http://www.pragprog.com/articles/tell-dont-ask "Procedural code gets information then makes decisions. Object-oriented code tells objects to do things." Perhaps the way I put it isn't clear enough because I don't know any OO programmers that disagree.
Bill K
There's a pretty substantial gap between applying the law of Demeter and deciding that any object that exposes properties is probably badly designed.
Robert Rossney
--Robert, Exactly! That's why I used all those qualifiers like "Could be" and "Indication" and a lot of exceptions. But in general, that's what that rule is saying--getting data is procedural, asking the object to do something is OO, (in general)
Bill K
+5  A: 

This is definitely the way to go.

public object MyProp {get; protected set;}

If you're on an older version of C# then this is the way to go.

private object _myProp;
public object MyProp
{
    get { return _myProp; }
    protected set { _myProp = value; }
}
Brendan Enrick
And if you're on a very old version of C#, you have to have a separate protected Set method :(
Jon Skeet