473,394 Members | 1,737 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,394 software developers and data experts.

why accessor methods?

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

Nov 15 '05 #1
6 2954
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]
- mv*@spam.guard.caspershouse.com

" Jason Shohet" <as****@hotmail.com> wrote in message
news:Oi**************@TK2MSFTNGP09.phx.gbl...
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

Nov 15 '05 #2
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.
" Jason Shohet" <as****@hotmail.com> wrote in message
news:Oi**************@TK2MSFTNGP09.phx.gbl...
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

Nov 15 '05 #3
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Oq**************@TK2MSFTNGP11.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

" Jason Shohet" <as****@hotmail.com> wrote in message
news:Oi**************@TK2MSFTNGP09.phx.gbl...
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


Nov 15 '05 #4
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
Nov 15 '05 #5
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...
Nov 15 '05 #6
Jason Shohet <as****@hotmail.com> wrote:
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.

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

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

Similar topics

6
by: jerrygarciuh | last post by:
Hello, I have been working for some time now on a PHP OOP database abstraction layer. Yes I know there are others out there which are maturing but I like reinventing this wheel. The task I...
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...
2
by: Todd A. Anderson | last post by:
I've inherited two "C++" code bases that I have to tie together and both of them make frequest use of public member variables in spite of all the information saying this limits flexibility. Well,...
22
by: Generic Usenet Account | last post by:
A lot has been said in this newsgroup regarding the "evil" set/get accessor methods. Arthur Riel, (of Vanguard Training), in his class, "Heuristis for O-O Analysis & Design", says that there is...
3
by: LuCk | last post by:
Can someone explain what these really are for example: ---------------------------------------------------------- void SetFrameRate(int iFrameRate) { m_iFrameDelay = 1000 / iFrameRate; }; ...
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...
8
by: AAJ | last post by:
Hi all I would like to have a class that can set/return values of different datatype via a single accessor, i.e. overload the accessor i.e. something like DateTime m_DateValue; string...
8
by: Tim Sprout | last post by:
Why is it considerd best practice to use Properties rather than Get and Set accessor methods? -Tim Sprout
2
by: rbjorkquist | last post by:
This is my first attempt at writing/using web services, so any and all comments will be greatly appreciated. With that said, I am also by no means saying this is the correct either. I have...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.