Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

public get ... private set ... HOW???

0 views
Skip to first unread message

John Lately

unread,
Apr 4, 2002, 11:32:56 AM4/4/02
to
Is there a nice, concise, easy way to declare a property that has a public
getter and a private setter?
Or does the access level always have to be identical?
Thanks in advance.

- John

Nicholas Paldino [.NET MVP]

unread,
Apr 4, 2002, 11:38:09 AM4/4/02
to
John,

There is no way to do this (in C#). You are right, the access level
always has to be identical.

The recommended way to get around this is to create methods that get/set
the property with different access levels.

Hope this helps.


--
- Nicholas Paldino [.NET MVP]
- nicholas...@exisconsulting.com

"John Lately" <jo...@dontspamme.com> wrote in message
news:ec%q8.15278$1a.43...@twister.kc.rr.com...

-glenn-

unread,
Apr 4, 2002, 1:18:29 PM4/4/02
to
John, this works in C#:

class Foo()
{
private int _number;
public int Number
{
get { return _number; }
}
private void SetNumber(int number)
{
// Perhaps do some validations on number.
_number = number;
}
}

SetNumber isn't a property but allows code in the class to set the Number
property's value.

You could always have the code directly set _number as well, but I like to
create a private method for this even if it is as simple as the one above
(in which case, the C# compiler will inline the code).

-glenn-

"John Lately" <jo...@dontspamme.com> wrote in message
news:ec%q8.15278$1a.43...@twister.kc.rr.com...

0 new messages