What is the purpose of Get and Set

Posted by Patel28rajendra under ASP.NET on 3/16/2012 | Points: 10 | Views : 2350 | Status : [Member] | Replies : 5
Hi

I want to know the purpose of using Get and Set and How it works

Any suggestion /answer is welcome.

Thanks

R D Patel


Responses

Posted by: Hmanjarawala on: 3/16/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi Rajendra,

Get is called Accessor and Set is called Mutators in .Net

in Oops to preserve DataAbstraction concept we keeps Data member private in class.
now to access that data members from outside world we need some mechanism through which we can use it.
for this we define property which contains Accessor and Mutator.
with this we can give tremendous data abstraction i.e.

class Example
{
//can read and assign from outside
Public Member1{get;set;}
//Read only member
Public Member2{get;}
//Read only for outside
Public Member{get;private set;}
}





Mark this as answer, if it helps you...........

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

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

Posted by: Patel28rajendra on: 3/16/2012 [Member] Starter | Points: 25

Up
0
Down
Hi

and how it will read data if we need to call or assign value for any where else

Thanks

R D Patel

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

Posted by: Hmanjarawala on: 3/16/2012 [Member] Bronze | Points: 25

Up
0
Down
From any where else:

Example e = new Example()
//Assign Value
e.Member1 = "Himanshu"
//Read value from Member2
object value = e.Member2;


Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

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

Posted by: Sksamantaray on: 3/16/2012 [Member] Silver | Points: 25

Up
0
Down
Hi Rajendra,
You can have a nice example here.
http://dotnetdud.blogspot.in/2010/03/accessors-and-mutators-in-c.html

Thanks,
Sanjay

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

Posted by: Kulprachi84 on: 3/16/2012 [Member] Starter | Points: 25

Up
0
Down
For eg:
public string CaptionText
{
set
{
this.Text = value;
}

}
public Color GridAlternatingColor
{
set
{
Grid.AlternatingBackColor = value;
}
}

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

Login to post response