| | |
How to use set/get ??
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
You don't need to use set or get. You could write functions named setFoo or getFoo instead of using a property Foo:
But that's a real pain -- you'd rather write
So instead C# has properties:
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.
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.
C# Syntax (Toggle Plain Text)
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:
C# Syntax (Toggle Plain Text)
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.
C# Syntax (Toggle Plain Text)
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.
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
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
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
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
0
#5 Oct 22nd, 2009
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
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
0
#6 Oct 22nd, 2009
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....
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....
Last edited by ddanbe; Oct 22nd, 2009 at 12:02 pm.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
0
#7 Oct 22nd, 2009
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#
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#
0
#8 Oct 22nd, 2009
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.
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
![]() |
Similar Threads
- Set, View, Change, or Remove Special Permissions for Files and Folders in Windows XP (Windows tips 'n' tweaks)
- Trying to set up wireless lan (Networking Hardware Configuration)
- beeing on dialup can i set up a outlook express email account (Windows NT / 2000 / XP)
- Airport Base Station Set Up??? (OS X)
- How can I set up my own email a/c with a domain name using microsoft outlook/express (Web Browsers)
- best chip set anyone? (Motherboards, CPUs and RAM)
- Set Precision in C++.NET? (C++)
- New Portal set up (DaniWeb Community Feedback)
Other Threads in the C# Forum
- Previous Thread: Connecting two computer using C# coding
- Next Thread: TextBox ENTER and ESC sound OnKeyPress
Views: 5045 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for properties, property







