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
Solved

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

Posted on 2012-03-30
6
202 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.

792 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