First i want to ask :
1. How will i decide which FIELD or METHOD will be PUBLIC or PRIVATE, i cant think about .
That's why , i always make all the FIELDS Private and all the methods PUBLIC and call every field using method. Is it a correct way?
2.While going though ENCAPSULATION, i met PROPERTY.
it has GET and SET , most of the time GET returns value and SET takes value. I can't get the program's approch. How can i approch the PROPERTY in my own code.
Plz..sum1 help me in getting these mesh hink practicle approch, so i can t:X
it has GET and SET , most of the time GET returns value and SET takes value. I can't get the program's approch. How can i approch the PROPERTY in my own code.
Plz..sum1 help me in getting these mesh hink practicle approch, so i can t:X

VulpesPosted Sep 16, 2012, 1:29 PM
private int _age;
public int get_Age()
{
return _age;
}
public void set_Age(int value)
{
if (value >= 0 && value <= 120)
{
_age = value;
}
else
{
throw new ArgumentException("Age is out of range");
}
}
If you don't like properties, then there's nothing to stop you writing your own 'get' and 'set' methods to access fields as you would do in languages such as C++ or Java which don't support properties.
However, in my view, properties are more convenient. To me this is cleaner:
Person p = new Person();
p.Age = 30; // implicitly calls property set
Console.WriteLine(p.Age); // implicitly calls property get
than this:
Person p = new Person();
p.set_Age(30);
Console.WriteLine(p.get_age());
I think you've more or less grasped what properties do but, if you have a background in other languages, may be finding it confusing when the get is called and when the set is called. If you think of properties as 'smart fields' then you may find it easier to grasp the difference.
Abhishek Kumar RaviPosted Sep 16, 2012, 12:30 PM
When i make a FIELD Private and then use a METHOD to return that value if someone call it.
Class Person
{
Private int _age;
public int ReturnAge()
{
return _age;
}
In case of PROPERTY.
public int Age
{
get
{
retutn _age;
}
set
{
_age=value;
}
}
Both of them are Diffrent or Same ?
What you have written :
In this case when i call it, Object_name.Age=18;
Then, VALUE will assigned with 18 and it goes to SET part first. There, it will check for condition or throw a error. And, when SET part is OVER then it will go for GET part where it will return the value of age (_age).
IS IT CORRECT , WHAT I 'VE EXPLAINED ABOVE?
** Even now, i m not clear where to use PROPERTY ?? And I can't get tht, what you have written in last !!
VulpesPosted Sep 16, 2012, 11:59 AM
This allows you to control access to the field and (for example) make sure that any value being assigned to it is reasonable:
class Person
{
private int _age;
public int Age
{
get { return _age; }
set
{
if (value >= 0 && value <= 120)
{
_age = value;
}
else
{
throw new ArgumentException("Age is out of range");
}
}
}
// rest of code in class
}
As far as methods are concerned, these normally perform some action - on the fields, parameters or other accessible data - and either return a value of some type or return no value ('void' method).
You need to consider whether you want your method to be callable from code outside the class - in which case make it public - or just to be used by code within the class - if so, make it private.
Also, if you think that other classes may derive from your class, then you might want to make private fields and methods protected rather than private. If you don't think it's a good idea for your class to be derived from, then you can make it 'sealed'.