Click here to Skip to main content
15,918,706 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
We are mostly suggested to not to use public variables, but to told to use property.
if i can do same thing with public variable, then why to use property?
Posted

Because if it is a public property, then you can change the internals of your class without affecting the outside world.

For example, if you have a class with a public variable "username" which holds data like "Mr Paul Jones" and "Mrs Alison Mary Smith", it works fine.
Until you realise that your class would work better if you used four name parts instead:
Status title;
string firstName;
string middleNames;
string lastName;
What happens to the outside world? Every class that used yours relies on "username": but it doesn't exist!

If username is a property, you can provide a getter and a setter that makes it work with the internal form, but looks just like the external form to legacy classes - nothing outside needs to change, you just have to test your revised class.
 
Share this answer
 
Comments
zingoraa 12-Apr-11 6:20am    
thank you a lot sir.
But still if in luckiest case nothing changes, yet again why property?
OriginalGriff 12-Apr-11 6:33am    
How do you know what changes you will need to make next week?
Design it to allow for changes to have the minimum impact. Properties allow you to help with that: if your variables are all private, then the only way they outside world can interact is via the mechanisms you specify. This helps encapsulate you class, and control the impact of changes later. Result: cleaner code, and higher reliability.
zingoraa 12-Apr-11 6:45am    
thx sir, i got the answer.
CodingLover 12-Apr-11 8:08am    
yes it is. specially with web developments you've to do lots of changes in early stages of the product. and there may be some modifications after the deployment is done.
Sergey Alexandrovich Kryukov 12-Apr-11 13:29pm    
Well, can be public, can be internal. Also, the point is side effect of property (getter/setter) not the access (this is not related).
--SA
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Apr-11 13:24pm    
Sure, a 5.
--SA
Tarun.K.S 12-Apr-11 14:46pm    
Thanks SA!
Because properties enforces encapsulation that is a fundamental OOP concept: they allow changing the object's behaviour without directly 'touching' the object internals.
:-)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Apr-11 13:24pm    
Short and to the point. My 5.
--SA
CPallini 12-Apr-11 13:50pm    
Thank you (by the way you gave the perfect definition for the C programming language) :-)
Sergey Alexandrovich Kryukov 12-Apr-11 15:01pm    
You look like a fun of this language. I'm not. :-)
--SA
CPallini 12-Apr-11 15:29pm    
Yes, I have a great fun with this language (not always, I've to admit. Going with Lua is fantastic when C becomes painful).
Sergey Alexandrovich Kryukov 12-Apr-11 16:25pm    
I like learning exotics languages, even marginal. To me, this is a good sign of a potentially good engineer, too.
--SA
use of public variables and public properties are following OOP concepts and is a generally accepted best practice. My advice is to familiar with them from the beginning.

The are many reasons to advice to use public property over a public variable. If you could search on the web. I feel following two are the most benefited.

1. Scalability. You can easily add code to the property to perform validation and related stuff.

2. Properties provide better status controlling (different versions may be) because of the ease-of scalability.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Apr-11 13:30pm    
This is good point, but may encapsulation feature should be mentioned -- side effect. My 5.
--SA
A property encapsulates a private variable. So the property itself controls
access to the variable and invoke business rules if needed.



Example:
int m_inning ;

If m_inning is public, it could be assigned any valid int value. But what if
you meant m_inning to hold only 1 thru 9?
The following property would enforce that business rule.



public int Inning()
{
set
{
if (value < 1 || value > 9) throw new InvalidArgumentException();
m_inning = value;
}
}


so for me its better to use properties
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 12-Apr-11 13:27pm    
Well, not only. You should not mix up access with the issue "property vs. field".
Getters and setters is the key (side effect), not access! Failure to explain it makes the Answer misleading.
This: "so for me its better to use properties" make me think you might not understand it well your self. Properties without fields are not possible, you cannot just "prefer" properties.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900