Solved

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

Posted on 2012-03-30
6
204 Views
Last Modified: 2012-04-16
Kindly quote example.
0
Comment
Question by:searchsanjaysharma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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