PC Review


Reply
Thread Tools Rate Thread

Attributes: I want my setter to be protected or private

 
 
Flipje
Guest
Posts: n/a
 
      31st Oct 2005
In my view, there is a major drawback to using attributes: the getter and
the setter have identical protection levels. But I usually want the getter
to be public and the setter to be protected or even private.

Example: I would have liked this to be possible:

int Thingy
{
public get { return mThingy; }
private set { mThingy = value; }
}

Unfortunately, it isn't. So now, instead of attributes, I use good old
GetThingy () and SetThingy () functions. I've been trying to find a decent
discussion about the subject, but it seems like I'm the only one who sees
having identical protection levels as a problem. So, am I missing something?
Is there some way to make attributes work the way I want?



 
Reply With Quote
 
 
 
 
Tim Wilson
Guest
Posts: n/a
 
      31st Oct 2005
This is possible in C# 2.0.

--
Tim Wilson
..NET Compact Framework MVP

"Flipje" <(E-Mail Removed)> wrote in message
news:4366365c$0$11067$(E-Mail Removed)...
> In my view, there is a major drawback to using attributes: the getter and
> the setter have identical protection levels. But I usually want the getter
> to be public and the setter to be protected or even private.
>
> Example: I would have liked this to be possible:
>
> int Thingy
> {
> public get { return mThingy; }
> private set { mThingy = value; }
> }
>
> Unfortunately, it isn't. So now, instead of attributes, I use good old
> GetThingy () and SetThingy () functions. I've been trying to find a decent
> discussion about the subject, but it seems like I'm the only one who sees
> having identical protection levels as a problem. So, am I missing

something?
> Is there some way to make attributes work the way I want?
>
>
>



 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      31st Oct 2005
Flipje,
As Tim suggests, in .NET 2.0 you can have different access levels on
properties.
http://msdn2.microsoft.com/en-us/library/75e8y5dd(en-US,VS.80).aspx

| public int Thingy
| {
| get { return mThingy; }
| private set { mThingy = value; }
| }


In .NET 1.0 & 1.1 I would normally use a readonly property, then create a
Set

| public int Thingy
| {
| get { return mThingy; }
| }

private SetThingy(int value) { ...}

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


"Flipje" <(E-Mail Removed)> wrote in message
news:4366365c$0$11067$(E-Mail Removed)...
| In my view, there is a major drawback to using attributes: the getter and
| the setter have identical protection levels. But I usually want the getter
| to be public and the setter to be protected or even private.
|
| Example: I would have liked this to be possible:
|
| int Thingy
| {
| public get { return mThingy; }
| private set { mThingy = value; }
| }
|
| Unfortunately, it isn't. So now, instead of attributes, I use good old
| GetThingy () and SetThingy () functions. I've been trying to find a decent
| discussion about the subject, but it seems like I'm the only one who sees
| having identical protection levels as a problem. So, am I missing
something?
| Is there some way to make attributes work the way I want?
|
|
|


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing private attributes from within classes Beorne Microsoft C# .NET 3 14th Jun 2007 04:31 PM
Namespace elements cannot be explicitly declared as private, protected, or protected internal newbie120@gmail.com Microsoft Dot NET 5 22nd Jun 2006 11:34 PM
Namespace elements cannot be explicitly declared as private, protected, or protected internal newbie120@gmail.com Microsoft C# .NET 4 19th Jun 2006 11:14 PM
error CS0200 on property's private setter geraldbauer@yahoo.com Microsoft Dot NET Framework 0 16th May 2006 04:07 PM
Attributes: I want my setter to be protected or private Flipje Microsoft Dot NET 5 10th Nov 2005 06:36 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:38 PM.