Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A question regarding encapsulation 1

Status
Not open for further replies.

utahjzz98

Programmer
Jan 9, 2003
69
US
If you only reference member variables inside the object they are contained in, do you need to create properties for those member variables, or can you just reference them directly?
 
If they are private and not visible outside the class, then it is more a question of taste and style than anything else.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
But if I make a property, then it will be visible outside the class correct? If it doesn't need to be accessible outside the class then I can just refer to it by it's name inside the local object instead of creating a property?
 
the language may have some bearing here. properties are most common in .net. assuming that is the language you are talking about a property is really just syntax sugar for get/set functions.
Code:
string Foo {get;set;}
gets compiled to
Code:
string _getFoo;
void _setFoo(string value);
if you think of properties as functions without () then you can decide if it makes sense to use protected/private properties.

if you need to encapsulate logic, even within a class itself, then a property makes sense. other wise a field works just as well.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top