How to use set/get ??

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 17
Reputation: 3pid is an unknown quantity at this point 
Solved Threads: 0
3pid 3pid is offline Offline
Newbie Poster

How to use set/get ??

 
0
  #1
Feb 27th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,055
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: How to use set/get ??

 
4
  #2
Feb 27th, 2009
You don't need to use set or get. You could write functions named setFoo or getFoo instead of using a property Foo:

  1. class Point {
  2. double x, y;
  3. public Point(double x, double y) {
  4. this.x = x;
  5. this.y = y;
  6. }
  7. public double GetX() { return x; }
  8. public void SetX(double x) { this.x = x; }
  9. public double GetY() { return y; }
  10. public void SetY(double y) { this.y = y; }
  11. }

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:
  1. class Point {
  2. double x, y;
  3. public Point(double x, double y) {
  4. this.x = x;
  5. this.y = y;
  6. }
  7. public double X {
  8. get { return x; }
  9. set { x = value; }
  10. }
  11. public double Y {
  12. get { return y; }
  13. set { y = value; }
  14. }
  15. }

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.
  1. class Point {
  2. public Point(double x, double y) {
  3. X = x;
  4. Y = y;
  5. }
  6. public double X { get; set; }
  7. public double Y { get; set; }
  8. }

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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: How to use set/get ??

 
2
  #3
Feb 27th, 2009
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
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 17
Reputation: 3pid is an unknown quantity at this point 
Solved Threads: 0
3pid 3pid is offline Offline
Newbie Poster

Re: How to use set/get ??

 
0
  #4
Feb 27th, 2009
Thanks!It really helped!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 3
Reputation: serban is an unknown quantity at this point 
Solved Threads: 0
serban serban is offline Offline
Newbie Poster
 
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,044
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 309
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Postaholic
 
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....
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 3
Reputation: serban is an unknown quantity at this point 
Solved Threads: 0
serban serban is offline Offline
Newbie Poster
 
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#
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,044
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 309
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Postaholic
 
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Reply

Tags
properties, property

Message:




Views: 5045 | Replies: 7
Thread Tools Search this Thread



Tag cloud for properties, property
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC