why accessor methods?

J

Jason Shohet

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, someone can do just as great harm by misusing a set { }
accessor method as just doing myObj.lastname = "Big Bird"; which he
could do in a second class, if the variable is public on the first class.
IOTW, what does keeping with the protected / accessor methods do for me.

Thanks
Jason Shohet
 
N

Nicholas Paldino [.NET/C# MVP]

Jason,

The idea behind properties is that you can perform some extra work when
a property is set/get, which you can not do with a field. It will allow you
to validate values when they are set (if you wish), or perform any other
operation.

Having the set/get accessors protected allows you to expose the property
to derived classes and will still allow you to have code that executes when
a property is set by the subclassed class. Just because a class derives
from you doesn't mean that you should give it unfettered access to you.

Hope this helps.
 
F

Fred Mellender

I think one of the purposes of an accessor is to provide the client with a
convenient interface (looks like assignment to/from a variable), while
keeping the option of implementing the accessor with a more complicated
function than just setting or returning a variable. Should the definition
of the accessor become more complex and need to be changed you can do so
without impacting the interface you have provided to the client. A simple
example would be the re-implementation of a "temperature" which might be
kept internally in degrees Kelvin, but returned/set in Centigrade.
 
N

news.microsoft.com

Its also useful for DLL Interop for wrapping and you can perform checks in
the property accessors and you can also use collections easier in a class.

get { return _collectionObj[current]; }

wana try that in a field?

or

get { QueryPerformanceCounter(ref i); return i; }


wana try that :D


Methods are for operations like Clear() or Reset() and props are for like
Count and CurrentItem etc..



Nicholas Paldino said:
Jason,

The idea behind properties is that you can perform some extra work when
a property is set/get, which you can not do with a field. It will allow you
to validate values when they are set (if you wish), or perform any other
operation.

Having the set/get accessors protected allows you to expose the property
to derived classes and will still allow you to have code that executes when
a property is set by the subclassed class. Just because a class derives
from you doesn't mean that you should give it unfettered access to you.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jason Shohet said:
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, someone can do just as great harm by misusing a set { }
accessor method as just doing myObj.lastname = "Big Bird"; which he
could do in a second class, if the variable is public on the first class.
IOTW, what does keeping with the protected / accessor methods do for me.

Thanks
Jason Shohet
 
J

Jason Shohet

Thanks Nicholas, you and Fred both gave good similar answers. Seems
accessors vs public properties in the class has nothing to do really with
scope, but really with not having to redo implementation if a bit of
business logic changes. I can do that business logic in my get or my set.
A second question:

if I do the get / set accessors in classA. Then classB inherits from
classA. If a client is using classB, can he call the accessors which were
defined in classA? Or do they need to be re-declared in every class that
inherits off the base classA?

Thanks
Jason Shohet
 
J

Jason Shohet

I just usually make member variables protected instead of private if I am
using accessors. For some reason if they are private, the accessors don't
work ! :) So I always keep them on protected...
 
J

Jon Skeet [C# MVP]

Jason Shohet said:
I just usually make member variables protected instead of private if I am
using accessors. For some reason if they are private, the accessors don't
work ! :) So I always keep them on protected...

Could you post an example of how they don't work? It's really a good
idea (IMO) to keep *all* non-constants private, and it certainly *can*
work, so it would be good to fix whatever's been going wrong in your
code before.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top