Solved

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

Posted on 2012-03-30
6
198 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

20 Experts available now in Live!

Get 1:1 Help Now