Solved

What is get and set and why do we use it.Please quote example

Posted on 2012-03-30
6
199 Views
Last Modified: 2012-04-16
Kindly quote example.
0
Comment
Question by:searchsanjaysharma
6 Comments
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 125 total points
ID: 37790018
Kindly see these:-
http://www.csharp-station.com/Tutorials/Lesson10.aspx
http://www.dotnetperls.com/property

Also read Properties chapter in Jeffery Ritcher's CLR VIA C# book(A Must Read!!!!).....
0
 
LVL 12

Assisted Solution

by:Ramkisan Jagtap
Ramkisan Jagtap earned 125 total points
ID: 37793357
0
 
LVL 12

Accepted Solution

by:
Anuradha Goli earned 125 total points
ID: 37795560
The accessor of a property contains the executable statements associated with getting (reading or computing) or setting (writing) the property.

syntax :

set {accessor-body}
get {accessor-body}

where:

accessor-body
The block that contains the statements to be executed when the accessor is invoked.

private string name;   // the Name field
public string Name   // the Name property
{
   get
   {
      return name;
   }
}
Employee e1 = new Employee();
...
Console.Write(e1.Name);   // The get accessor is invoked here
-------------------
public string Name
{
   get
   {
      return name;
   }
   set
   {
      name = value;
   }
}

e1.Name = "Anu";   // The set accessor is invoked here
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Assisted Solution

by:navneethegde
navneethegde earned 125 total points
ID: 37796074
Hi!

1.You have a class
class MyClass {}

2.and then you decide to have a variable
class MyClass {
private string myVariable;

}

3.then you decide to have it public, so it can be accessed publicly
class MyClass {
public string myVariable;
}

4. Now the one using your public variable, is assigning all type of values
   ex : Your public variable is  
          public int myAge
         any one is assigning values like 500, 1002 values to this varaible, hnec you want to put some check
         and you introduce
        set {}
       which helps you   validate the stuff before assigning the value

        On same path user should be only allowed to set the value and and should not be able to read it.
       or want to let user get the value of your variable in some other form and hence we have
       get{}

5. Therefore the birth of Property with get{} and set{}

Thanks!
0
 

Author Comment

by:searchsanjaysharma
ID: 37850076
ok
0
 

Author Closing Comment

by:searchsanjaysharma
ID: 37850078
ok
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now