473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

accessor pairs - bad examples abound?

How come a lot of the examples I have seen on the net define accessor pairs
within a class, and then only ever use the accessor outside the class? When
used within the class the variable is accessed directly.

Is there something I'm missing about this? To reduce code re-writing it is
(I thought) standard practice to use accessor pairs in the class that
defines them as well as outside.

This becomes apparent if you need to perform checks on a new value before
assigning it to the variable. If you have 10 places where the variable is
directly accessed within the class then you would have 10 places to update
your code. If using an accessor pair then you only have 1 place to update
you code.

Correct me if I'm wrong or missing something about C# accessor pairs.

Rich.
Nov 16 '05 #1
11 1661
Kavvy <bl*@debla.bla> wrote:
How come a lot of the examples I have seen on the net define accessor pairs
within a class, and then only ever use the accessor outside the class? When
used within the class the variable is accessed directly.

Is there something I'm missing about this? To reduce code re-writing it is
(I thought) standard practice to use accessor pairs in the class that
defines them as well as outside.


That's *a* convention (and one which I like) but I don't think it
really counts as standard practice yet.

One thing I wish C# had was a way of declaring that the variable could
*only* be accessed through the property, to enforce this convention.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
"Kavvy" <bl*@debla.bla> wrote in
news:lT*********************@news-text.cableinet.net...
How come a lot of the examples I have seen on the net define accessor
pairs
within a class, and then only ever use the accessor outside the class?
When
used within the class the variable is accessed directly.
IMO The most imporant aspect of properties is that they hide implementation
details of a class. There's no need to hide implementation details from the
implementation.
Is there something I'm missing about this? To reduce code re-writing it is
(I thought) standard practice to use accessor pairs in the class that
defines them as well as outside.


Of course you should use property accessors if you can reuse code that way.
But this really depends on what the accessors are doing. Just a few
examples:
- if the set-accessor updates some DB or GUI you might want to do one bulk
update for a complex operation, instead of many little updates for each
changed property
- a complex operation might change two of more variables, so the object
state might indeed be inconsistent during the operation
- it is also common that accessors only return interfaces instead of classes
(e.g. IEnumerable instead of ArrayList), so the actual implementation can be
changed without changing other classes; Operations inside the class might
need full access on the object.

Niki
Nov 16 '05 #3
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote:
One thing I wish C# had was a way of declaring that
the variable could *only* be accessed through the
property, to enforce this convention.


This seems difficult, because (to the compiler) there is no explicit
association between a property and the variable that it affects.
Indeed, setting a property might affect multiple variables in the
class, or even none (e.g. a property that sets another object's
property).

In the case of "simple properties" (ones that are just get/set
wrappers over private variables), I'd like to be able to declare them
with a single construct - perhaps using a new "property" modifier -
but this is obviously less flexible than the current system, where
accessors can contain arbitrary code.

P.
Nov 16 '05 #4
"Paul E Collins" <fi******************@CL4.org> wrote in
news:ch**********@sparta.btinternet.com...
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote:
One thing I wish C# had was a way of declaring that
the variable could *only* be accessed through the
property, to enforce this convention.


This seems difficult, because (to the compiler) there is no explicit
association between a property and the variable that it affects.
Indeed, setting a property might affect multiple variables in the
class, or even none (e.g. a property that sets another object's
property).


You could declare the variable somewhere inside the property.
Like this

public int MyProperty
{
int myProperty;
get { return myProperty; }
set { myProperty = value; }
}

Actually I think instance-variables that are only visible to a scope of a
function or property would be handy in many cases.

Niki

Nov 16 '05 #5
"Paul E Collins" <fi******************@CL4.org> wrote in
news:ch**********@sparta.btinternet.com...
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote:
One thing I wish C# had was a way of declaring that
the variable could *only* be accessed through the
property, to enforce this convention.


This seems difficult, because (to the compiler) there is no explicit
association between a property and the variable that it affects.
Indeed, setting a property might affect multiple variables in the
class, or even none (e.g. a property that sets another object's
property).


You could declare the variable somewhere inside the property.
Like this

public int MyProperty
{
int myProperty;
get { return myProperty; }
set { myProperty = value; }
}

Actually I think instance-variables that are only visible to a scope of a
function or property would be handy in many cases.

Niki
Nov 16 '05 #6
Niki Estner <ni*********@cube.net> wrote:
This seems difficult, because (to the compiler) there is no explicit
association between a property and the variable that it affects.
Indeed, setting a property might affect multiple variables in the
class, or even none (e.g. a property that sets another object's
property).


You could declare the variable somewhere inside the property.
Like this

public int MyProperty
{
int myProperty;
get { return myProperty; }
set { myProperty = value; }
}

Actually I think instance-variables that are only visible to a scope of a
function or property would be handy in many cases.


That's exactly what I'd be suggesting, yes.

Internally it would compile just to a private variable, but the
compiler would know not to let the class access that variable outside
the property.

You'd probably need some name-munging in there as well, and there are
"difficulties" involving serialization etc, but it's still something
I'd like to see explored.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7


You'd probably need some name-munging in there as well, and there are
"difficulties" involving serialization etc, but it's still something
I'd like to see explored.

Hmm, what kind of difficulties do you see with serialization, specificatlly? --
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #8
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
You'd probably need some name-munging in there as well, and there are
"difficulties" involving serialization etc, but it's still something
I'd like to see explored.


Hmm, what kind of difficulties do you see with serialization, specificatlly?


To be honest, I can't remember at the moment - but I know that last
time I brought this up, someone gave some reasons why you'd need to
access the variables outside the properties.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
> You'd probably need some name-munging in there as well, and there are
> "difficulties" involving serialization etc, but it's still something
> I'd like to see explored.
Hmm, what kind of difficulties do you see with serialization,
specificatlly?


To be honest, I can't remember at the moment - but I know that last
time I brought this up, someone gave some reasons why you'd need to
access the variables outside the properties.


Hrmm, that is a curious question. Considering a matched pair of get\set
accessors, accessing the data couldn't be very hard.

I suppose something like
int field;
int ActualValue;
{
get
{
return field*2;
}
}

would be a pain, and the subgrouping does nothing for small groups of
properties that require access to the same field, but solving that is alot
more indepth.

A custom accessor might fix the serialization problems, say
serialize
{
return field;
}

and limit access to serialize_ to some method(s) that are marked as
serialization methods, but that does require a change to the language. --
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #10
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
To be honest, I can't remember at the moment - but I know that last
time I brought this up, someone gave some reasons why you'd need to
access the variables outside the properties.
Hrmm, that is a curious question. Considering a matched pair of get\set
accessors, accessing the data couldn't be very hard.

I suppose something like
int field;
int ActualValue;
{
get
{
return field*2;
}
}

would be a pain


Of course, making such a field "ultra-private" for the rest of the
class would make it rather difficult to be useful anyway. Perhaps it
should only be available for read/write properties?
and the subgrouping does nothing for small groups of
properties that require access to the same field, but solving that is alot
more indepth.

A custom accessor might fix the serialization problems, say
serialize
{
return field;
}
Hmm... possibly. I suspect that constructors might also need access - I
can imagine some cases where you want the actual property to be read-
only, but set up during construction.
and limit access to serialize_ to some method(s) that are marked as
serialization methods, but that does require a change to the language.


Well, it's all requiring a change to the language anyway :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #11
I suppose something like
int field;
int ActualValue;
{
get
{
return field*2;
}
}

would be a pain
Of course, making such a field "ultra-private" for the rest of the
class would make it rather difficult to be useful anyway. Perhaps it
should only be available for read/write properties?


Probably, except for readonly+constructor initalized as you mention below. I
actually forgot to write a set for that, but no matter, it is a potential
problem
A custom accessor might fix the serialization problems, say
serialize
{
return field;
}


Hmm... possibly. I suspect that constructors might also need access - I
can imagine some cases where you want the actual property to be read-
only, but set up during construction.


Yes, that is a very real issue to consider
and limit access to serialize_ to some method(s) that are marked as
serialization methods, but that does require a change to the language.
Well, it's all requiring a change to the language anyway :)


LOL, yes, but a less drastic one. The major issue I'm concerned with is how
do you express the difference between direct field access and property
access if the field is scoped and accessible using a special accessor. You
could go whole hog and allow for a method-style modifier:
fieldof\valueof (ActualValue)
or you could simply allow access to the variable in select spots, or perhaps
allow for some other style of access(a psuedo-FieldValue field on the
property, perhaps?).

I don't like scoping a variable lexically and then allowing it to be
accessed in constructors and *some* methods without some sort of special
operation, it just isn't consistent. If the variable looks like its well
scoped, it just seems wrong to only actually enforce a weak scope. Without a
special operator(or set thereof), I don't think it would be terribly easy to
do. But, I could be wrong.

One idea I had toyed with was full blown acls, so you could do
int field
allow read in all,
allow write in ActualValue,Serialize, .ctor;

but that is alot of typing, even if it does bring exact accessibilty
granularity up quite a bit, beyond anything regular languages use, anyway.
That doesn't mean its a good idea, ;).
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: mirandacascade | last post by:
When I look at how classes are set up in other languages (e.g. C++), I often observe the following patterns: 1) for each data member, the class will have an accessor member function (a...
3
by: LuCk | last post by:
Can someone explain what these really are for example: ---------------------------------------------------------- void SetFrameRate(int iFrameRate) { m_iFrameDelay = 1000 / iFrameRate; }; ...
1
by: Pol Bawin | last post by:
Hi, I have a objectA with a property that return an object B (accessor GET only) I have defined an ExpandableObjectConverter on the type B and overrided CanConvertTo, CanConvertFrom, ... The...
6
by: Jason Shohet | last post by:
I have a class with protected variables and some accessor methods, , get, set ... Maybe I have a brain blockage today but I'm thinking, why not just make those variables public. After all,...
7
by: Javaman59 | last post by:
This is probably common knowledge to .Net gurus, but please bear with me while I share a discovery with the group... I needed to create a public lock on a class, as follows... class Locked {...
7
by: Tenacious | last post by:
I have been programming using C# for over a year now and I still wonder what is the importance of using accessor methods when the property is read-write. It seems easier to just make it a public...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
3
by: Allerdyce.John | last post by:
I have a design type of quesiton. What is the advantages of using accessor (a getter/setter method) instead of making the attribute 'public'? If a class has public accessor (a getter/setter...
15
by: mcjason | last post by:
I saw something interesting about a grid pair puzzle problem that it looks like a machine when you find each that work out the way it does and say with all the others that work out the way they...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.