Getter Setter Method [Resolved]

Posted by Akiii under C# on 6/29/2011 | Points: 10 | Views : 11982 | Status : [Member] | Replies : 12
Hi,
Can anyone explain it to me what is a getter setter thing ?
Why to we write those in a class and what is the use of it ?

Any help is appreciated......

Thanks and Regards
Akiii




Responses

Posted by: Ndebata on: 6/29/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
In simple a property allow you to embed your own logic with the field of the Class.
Here logic mean anything. :)
1.
It can be just allow to only RETRIEVE ONLY or READ , you
in this case you will implement only get .
public int ID

{
get;
}
private string _somevalue;
public string SomeValue
{
get { return _somevalue; }
}

2.
Similarly it will only allow you to ASSIGN or SET the value.
in this case you will implement only set .
public int ID

{
set;
}
private string _somevalue;
public string SomeValue
{
set { _somevalue = value; }
}

3.
Expose a part of the Controls Field.
Lets say in a Form one Label Control is there and you are interested to access/ modify its Text Property ONLY.
in this you could do like this
public String LabelValue

{
get { return lblForm.Text; }
set { lblForm.Text = value; }
}

4.
You can perform some validation also
Lets say some property called ID should not be negative.
public static int ID

{
set
{
if (value < 0)
throw new ArgumentException("Negative values are not allowed");
}
}


Similarly many more....
To know more about property
http://msdn.microsoft.com/en-us/library/w86s7x04.aspx

Thanks,
Narayan

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Susanthampy on: 6/30/2011 [Member] [MVP] Bronze | Points: 50

Up
0
Down

Resolved
Example,

class TimePeriod

{
private double seconds;

public double Hours
{
get { return seconds / 3600; }
set { seconds = value * 3600; }
}
}


class Program
{
static void Main()
{
TimePeriod t = new TimePeriod();

// Assigning the Hours property causes the 'set' accessor to be called.
t.Hours = 24;

// Evaluating the Hours property causes the 'get' accessor to be called.
System.Console.WriteLine("Time in hours: " + t.Hours);
}
}
// Output: Time in hours: 24



Regards,
Susan

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nithadeepak on: 7/2/2011 [Member] Bronze | Points: 50

Up
0
Down

Resolved
Hi Akiii,

Explanation to your below comment.....
"i think by using getter setter method we can initialize the private property of the class....."

By using the property feature(get; and set; methods) of c#, we are not only initialize a class variable , but we make a private class variable public.
For ex: we cannot access a private variable using the object of a class.Only the method of a class can access the private variables.

But when we declare a property , we can access the private variable through that property....

That means,

class ex

{
int _x;
public int X
{
get
{
return -x;
}
set
{
_x=value;
}
}
}
Now we can access that _x using the property...
ex obj=new ex();
obj.X=12;
It will assign value 12 to the variable _x;
also

we can get the value of _x by int y=obj.X;

so where we can use obj.X where ever we want(instead of _x).
_x can be used only by the methods of that class.
So for using a private variable outside the methods of a class we are using properties. Properties are declared public.


Nitha Deepak

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Susanthampy on: 6/30/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi Akii,

Refer this links,

http://www.csharp-station.com/Tutorials/Lesson10.aspx

http://www.devasp.net/net/articles/display/825.html



Regards,
Susan

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Susanthampy on: 6/30/2011 [Member] [MVP] Bronze | Points: 25
Posted by: Lakn2 on: 6/30/2011 [Member] Starter | Points: 25

Up
0
Down
try this

http://www.csharp-station.com/Tutorials/Lesson10.aspx

Thanks&Regards
LakshmiNarayana Nalluri.

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 7/1/2011 [Member] Bronze | Points: 25

Up
0
Down
@susan....

thanks for your sample code......
i think by using getter setter method we can initialize the private property of the class.....

i mark ur answer...

Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 7/1/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi narayan...

your explanation is quite good especially the condition part in the set method.....

I mark urs as an answer.....
Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 7/1/2011 [Member] Bronze | Points: 25

Up
0
Down
hi Lakn2 ....

thanks for your suggestion.....

Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 7/2/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi
nice explanation nitha.......

thanks for your time....

Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bronko on: 8/14/2011 [Member] Starter | Points: 25

Up
0
Down
Hi there,

in case you are able to understand german the following link explains the topic in a Video-Tutorial:

http://www.experdios.de/video/c-sharp-getter-setter

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 8/14/2011 [Member] Bronze | Points: 25

Up
0
Down
hi bronko...

thank you for your help but i dont understand german.....

Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response