Home All Groups Group Topic Archive Search About
Author
27 May 2006 1:33 PM
vze1r2ht
Is there a way in C# for a property to be Write-enabled from a certain
class and readonly from outside classes?

For an example, I have two objects User and UserManager.

I want certain properties that should be readonly be set by the
UserManager. Other classes should only be able to read that property.

Such properties include, User.Exists and User.DateCreated etc.

Author
27 May 2006 1:40 PM
Barry Kelly
vze1r***@verizon.net wrote:

> Is there a way in C# for a property to be Write-enabled from a certain
> class and readonly from outside classes?

The usual solution here is to use internal visibility for things that
need to be writable by classes in the same assembly. For example:

---8<---
class Foo
{
    private int _value;
    public int Value
    {
        get { return _value; }
        internal set { _value = value; }
    }
}
--->8---

-- Barry


AddThis Social Bookmark Button