954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to use set/get ??

hey guys,
I`m a beginner in c# and I have a problem in using set/get in classes.
actually I didn`t get why do we use them,can anyone please introduce them briefly for me?

3pid
Newbie Poster
17 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

You don't need to use set or get. You could write functions named setFoo or getFoo instead of using a property Foo:

class Point {
    double x, y;
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
    public double GetX() { return x; }
    public void SetX(double x) { this.x = x; }
    public double GetY() { return y; }
    public void SetY(double y) { this.y = y; }
}


But that's a real pain -- you'd rather write pt.Y = 3; and be able to write things like pt.Y += 5; , instead of pt.SetY(pt.GetY() + 5); .

So instead C# has properties:

class Point {
    double x, y;
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
    public double X {
        get { return x; }
        set { x = value; }
    }
    public double Y {
        get { return y; }
        set { y = value; }
    }
}


Inside the setter, the keyword 'value' is the variable containing the value that is getting assigned to the property Y.

The pattern of having properties directly backed by fields is so common that in C# 3, shortcut syntax was added.

class Point {
    public Point(double x, double y) {
        X = x;
        Y = y;
    }
    public double X { get; set; }
    public double Y { get; set; }
}


There are a few reasons to use properties, instead of public fields. One is that properties can be virtual. Another is that you can make the setters for a property private. Another is that properties have a 'special' meaning to things that inspect classes at runtime. There are frameworks for conveniently talking to databases and for reading and writing objects to and from XML and all sorts of other things -- and they automatically look at the object's properties (and not private fields or other things) to see how to do their job.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

A reason to use get/set is so that your class contains control over the input

For example

Exam results shoul dbe between 0-100

No exact type exists to hold only 0-100, so, if the class has a set which validates the data, it cant ever have an issue where the data has been rigged

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

Thanks!It really helped!

3pid
Newbie Poster
17 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

In the above class:

class Point {
    public Point(double x, double y) {
        X = x;
        Y = y;
    }
    public double X { get; set; }
    public double Y { get; set; }
}


the copiler will throw an error as no implementation is provided to set and get in the:
public double X { get; set; }
public double Y { get; set; }
lines.

Why is that???
Thank you

serban
Newbie Poster
3 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

Hi Serban, welcome here.
Did you read the excellent explanation of Rashakil Fol to it's full extend?
The handy syntax public double X { get; set; } can only be used if you have version 3.x of C#. My guess is you use a lower version. If that is not the case then post your question in a new thread.

EDIT: BTW the implementation IS provided if you use this syntax. The compiler does all the dirty work for you. One of the reasons why I love C#. In a sense I'm a lazy person....

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

Thank you DDanbe ...

That means that any methods can be written like that?
Eg... constructors:

public ClassConstructor(); instead of
public ClassConstructor(){}
OR
are these the same
public ClassConstructor(int param);
public ClassConstructor(int param)
{
_localClassInteger = param;
}


Sorry I dont have 3.0 on my machine to test these ...

Thank you again ....
Thread: How to use set/get ??
Forum: C#

serban
Newbie Poster
3 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

Please don't post NEW questions on an OLD thread!
This can become very confusing to other users.
So, I won't answer your questions here.
Post a new thread with your question instead, you can always refer to old threads by using their http addresses or give a full explanation.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: