[Mono-list] C# General Question...

Jonathan Pryor jonpryor@vt.edu
13 Jan 2003 09:51:06 -0500


When in doubt...  Look at the Base Class Library.  Of interest is
System.Console, which has In, Out, and Error get properties, but to set
them you use SetIn, SetOut, and SetError.

The short answer is that C# doesn't allow different protection levels
for properties.  Period.

I'm sure they had a reason for that, but it probably boils down to "we
thought this would be confusing for users of the class" (which it could
be, depending on circumstances).  There may be other reasons, but I
don't know.

If you *really* want to use a property, name it differently:

	class A {
		private int data;
		public int Data {
			get {return data;}
		}

		protected int InternalData {
			get {return data;}
			set {data = value;}
		}
	}

 - Jon

On Mon, 2003-01-13 at 08:08, Eric J. Peters wrote:
> Hi-
> 
> class A is the simplest example of the protection levels I could come up with, but doesn't
> really do anything to explain the thought behind the question, I apologize.
> 
> Here it is again:
> 
> > > class A {
> > >    private int m_data = 1;
> > >    public int Data {
> > >       get { return m_data; }
> > >    }
> > >    protected int Data {
> > >       set { m_data = value; }
> > >    }
> > > }
> 
> First, the accessors are necessary in the real implementation because the act of getting
> and setting isn't trivial (as it is in class A).  Besides, accessors are good practice,
> especially when anticipating code reuse; which is also expected in the real implementation.
> 
> The property 'Data' is publically gettable, but not publically settable --  a read-only
> property for anything but child classes.
> 
> Since the setting is non-trivial, I don't want to allow subclasses to directly set the
> member, but to use the property setter.  True, I could just make the setter public, but
> that just ain't right from a overly anal-retentive software engineering perspective.
> 
> Currently to accomodate all of that, I have a 'protected setData(int)' member to allow for
> setting the data by the class (and it's subclasses).  However, that is not consistant with
> the accessor model that C# uses, and is just wrong for the same SE perspective mentioned
> above.
> 
> I scanned the ECMA spec, but came up empty handed.  Anyone know of a way to code the
> get and set accessors with different protection levels?
> 
> 	-Eric.
> 
> On Sat, 11 Jan 2003, Dan Guidara wrote:
> 
> > On Fri, 2003-01-10 at 19:09, Eric J. Peters wrote:
> > > Hi-
> > > 
> > > 	While this isn't a mono-specific question, I think this list, being
> > > very technical and C# based, is the best place I know of to ask.
> > > 
> > > 	Imagine a class as shown here:
> > > 
> > > 
> > > class A {
> > >    private int m_data = 1;
> > >    public int Data {
> > >       get { return m_data; }
> > >    }
> > >    protected int Data {
> > >       set { m_data = value; }
> > >    }
> > > }
> > > 
> > > 
> > > 	Obviously, this is a compile-time error.  My specific question is if
> > > there is a language mechanism to allow for different access levels between the
> > > two different accessors.  If not, then I would be interested in hearing why
> > > not -- it seems like a significant oversight to me.
> > > 
> > > 	Thanks,
> > > 	-Eric.
> > > 
> > > _______________________________________________
> > > Mono-list maillist  -  Mono-list@ximian.com
> > > http://lists.ximian.com/mailman/listinfo/mono-list
> > > 
> > 
> > 
> > Eric,
> > 	I am not sure why you would want to do that. Its my understanding that
> > Properties are public in their nature to allow an interface for setting
> > internal variables in a controlled manner from extranal classes, etc. I
> > quess you could create functions with the desired protection levels to
> > read and write the values. I am not sure where the oversight comes into
> > it at all. 
> > 
> > Dan
> > 
> > 
> > 
> > _______________________________________________
> > Mono-list maillist  -  Mono-list@ximian.com
> > http://lists.ximian.com/mailman/listinfo/mono-list
> --
> Moore's Law has been the name given to everything that changes exponentially.
> I saw, if Gore invented the Internet, I invented the exponential.
> 
> 	-Gordon Moore
> 
> _______________________________________________
> Mono-list maillist  -  Mono-list@ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list