Properties

Miles _______ at _______.____
Thu Jan 8 17:21:05 PST 2009


Chad J wrote:
> I actually like the idea.
> 
> (...)
> 
> int nTimesVarRead = 0;
> 
> public property int var
> {
>     get
>     {
>         nTimesVarRead++;
>         return internalValue;
>     }
> 
>     set { internalValue = $; }
> }

That defeats one of the purposes of properties. A property is usually an
accessor for some member variable. You have two concepts: the property
and the member variable; they MUST be declared separately, since they
may have different attributes.

For example, the underlying member variable may have any of public,
protected or private visibility, while the property may also have any of
public, protected or private visibility. Usually, the member variable is
either protected or private, while the property is either public or
protected.

Also, the class itself, and any inner scopes, may want to access the
underlying member variable directly, without going through the property
accessors.

> Perhaps, since the compiler knows the name of the property, the name of
> the generated internal value could just be the name of the property:
> 
> public property int var
> {
>     get { return var; }
>     set { var = $; }
> }

That is the question... the property is public, but var is private or
protected? How does the class access var without triggering the property
accessors?



More information about the Digitalmars-d mailing list