Properties with different accessors?

  • Thread starter Thread starter A Ratcliffe
  • Start date Start date
A

A Ratcliffe

Is it possible to create a set property which has a different accessibility
than the get property?

For example, I have a private field in a base class. The Get property should
be public so that anyone can see the value of the field. However the Set
property should be protected and non-overridable (since it does various
additional calculations/actions based on the new value) and only derived
classes should be able to Set a new value.

Hope that makes sense,

Thanks in advance for any help,

Ann-Marie Ratcliffe
 
Is it possible to create a set property which has a different accessibility
than the get property?

No. But it's a commonly requested feature for future versions.



Mattias
 
Figures ;(

I guess the best work-around is to have the Property as read-only for the
hoi-polloi, and then have a protected Method for the derived classes, but it
all seems like a two'n'eight for what could be so simple.

Thanks for your quick reply,

Yours,

Ann-Marie Ratcliffe

P.S. If there is a neater way than I've suggested above I'd love to hear it.
 
A Ratcliffe said:
I guess the best work-around is to have the Property as read-only for the
hoi-polloi, and then have a protected Method for the derived classes, but it
all seems like a two'n'eight for what could be so simple.

Yes, it's a pain - and frankly, I'm surprised it's one which didn't get
fixed before v1.0. It seems such an obvious thing to want to do...
maybe there are problems with it that I'm not aware of.

Having either a readonly property and a mutator method, or vice versa
(private property, public accessor), or just having two methods are
indeed the options at the moment.

Fortunately, not only has this been requested for the next version, but
apparently it's going to happen, too :)
 
Thanks for the reassurance. I hope .NET Framework 1.2 comes soon.

Yours,

Ann-Marie Ratcliffe

PS. As noted in an earlier message, congratulations on your MVP status. One
day I hope to gain that honour, but somehow I think I've got a lot to learn
and much help to give before I see the light at the end of the tunnel.
 
A Ratcliffe said:
Thanks for the reassurance. I hope .NET Framework 1.2 comes soon.

I don't know what version number Whidbey will be, but I suspect it'll
be v2.0, given the number of big enhancements (eg generics) to it.
Unfortunately, I don't think it'll be here for a while :(
PS. As noted in an earlier message, congratulations on your MVP status. One
day I hope to gain that honour, but somehow I think I've got a lot to learn
and much help to give before I see the light at the end of the tunnel.

Fortunately I've found that learning and giving help go hand in hand :)
 
I believe this is more issue of c# than of .NET as a platform. So hopefully
the next version of the compiler will let us to that.
B\rgds
100

A Ratcliffe said:
Thanks for the reassurance. I hope .NET Framework 1.2 comes soon.

Yours,

Ann-Marie Ratcliffe

PS. As noted in an earlier message, congratulations on your MVP status. One
day I hope to gain that honour, but somehow I think I've got a lot to learn
and much help to give before I see the light at the end of the tunnel.
 
100 said:
I believe this is more issue of c# than of .NET as a platform. So hopefully
the next version of the compiler will let us to that.

No, it's part of .NET itself - the CLI specification itself currently
states that a property must have the same access for access as
modification, although I can't remember exactly where.
 
Yes you are right
"CLS Rule 25: The accessibility of a property and of its accessors shall be
identical."
 
Different property accessors are allowed in Managed C++, the restriction is at the CLS level.

Willy.
 
Back
Top