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

Eric J. Peters erpeters@informationinplace.com
Mon, 13 Jan 2003 08:08:37 -0500


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