private fields, public properties...why??  
Author Message
IS dude





PostPosted: Visual C# General, private fields, public properties...why?? Top

Why is it preferred to have private fields and use public properties to access and/or set the variable values

Some of the books i have read do explain why...but only vaguely. Does anyboday know the real 'life threatening' type reason why we should do things this way

Thanks



Visual C#13  
 
 
swemaniac





PostPosted: Visual C# General, private fields, public properties...why?? Top

Basically because you have much more control over what's being assigned to the private member (and returned). I guess you could say it's a fancy way of implementing the error checking functionality of regular get/set methods but without having to use the method structure, just treat it as it would be your actual private member. Not only to prevent errors but to perform specific tasks relevant only to the member in question (see below). It's neat!

private int quality;

public Quality
{
set
{
quality = ((value > 100) 100 : ((value < 1) 1 : value));
}
}

I personally seldom use properties however, I tend to use traditional get/set methods. (C++ war wound). One scenario where I do use properties however is when I want to display them in a PropertyGrid and let the user change the values directly.

As for assigning ANY members different access levels.. It's easy to make stupid typos that won't show up as an error not even in run time, but make the program act unexpectedly. Like assigning a value to a member instead of testing it for equality. (= vs ==). Or restricting access in derived classes etc..

Hope it helped
Cheers


 
 
IS dude





PostPosted: Visual C# General, private fields, public properties...why?? Top

thank you very much. your explanation is very clear. I am still struggling with the concept of properties that look more like methods. I understand why they are called properties but the fact that these properties use verbs (get and set) instead of nouns kinda confuse me and my friends.

Now, the point you made about having more control over your code, I wonder why you would have less control over the code if you don't have this public / private arrangement. I mean, since we are all developing our applications within the Visual Studio IDE, why do we still need to be concerned about encapsulation Can an unscrupulous programmer gain access and change our code if we make everything public even after the program has been compiled


 
 
ahmedilyas





PostPosted: Visual C# General, private fields, public properties...why?? Top

well there is that too in a sense but its just best practice/better design. Say you are working on a large project between you and your developers. What happens if a developer accidently, without realizing, sets a value of a property of a class to something which you do not want, or which can cause severe problems during your execution of your application, such as calculation or changing a bool value to true instead of having it to false (or whatever) you would be wondering out why on earth it happened or why did they do it - having properties gives you more control on what you can get or set from a private variable in that class.

you might have 2 classes, one class which creates a Person object, the other class to just analyze the Person object - you dont want this analyzing class to modify properties/variables of the Person class right you just want them to read the variables/properties, not set them otherwise not only is it defeating the purpose of "analyzing" the object but also the user would be like "uh...I didn't enter this...why did it change "

you may also only want to pass a class (ClassA) into another class(ClassB) but dont want that class (ClassB) to modify any public variables of this class (ClassA). Therefore having a public property can allow you to control how you wish the caller to access your variables in ClassA



 
 
IS dude





PostPosted: Visual C# General, private fields, public properties...why?? Top

Okay thank you. so what you are saying is that the purpose of encapsulation is mainly to prevent human errors from occuring
 
 
AdrianJMartin





PostPosted: Visual C# General, private fields, public properties...why?? Top

add verastility to the list of reasons why.

you could add a property to a class that takes a single type as parameter and the value off that parameter could effect several member fields.

eg.

foo.Position = "200,200" ;

Position is a property of foos type that takes a formatted string, verifys the value and stores the result in the private field x and y.

Oh and it fires an event to any listners that the position of foo has changed!