943,718 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 100453
  • C# RSS
Feb 27th, 2009
0

How to use set/get ??

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
3pid is offline Offline
17 posts
since Apr 2008
Feb 27th, 2009
4

Re: How to use set/get ??

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

C# Syntax (Toggle Plain Text)
  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:
C# Syntax (Toggle Plain Text)
  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.
C# Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 1135
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Feb 27th, 2009
2

Re: How to use set/get ??

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
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Feb 27th, 2009
0

Re: How to use set/get ??

Thanks!It really helped!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
3pid is offline Offline
17 posts
since Apr 2008
Oct 22nd, 2009
0
Re: How to use set/get ??
In the above class:

C# Syntax (Toggle Plain Text)
  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. }

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
Last edited by peter_budo; Apr 20th, 2011 at 2:47 am. Reason: Adding code tags to old post
Reputation Points: 10
Solved Threads: 0
Newbie Poster
serban is offline Offline
3 posts
since Oct 2009
Oct 22nd, 2009
0
Re: How to use set/get ??
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.
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Oct 22nd, 2009
0
Re: How to use set/get ??
Thank you DDanbe ...

That means that any methods can be written like that?
Eg... constructors:
C# Syntax (Toggle Plain Text)
  1. public ClassConstructor(); instead of
  2. public ClassConstructor(){}
  3. OR
  4. are these the same
  5. public ClassConstructor(int param);
  6. public ClassConstructor(int param)
  7. {
  8. _localClassInteger = param;
  9. }

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

Thank you again ....
Thread: How to use set/get ??
Forum: C#
Last edited by peter_budo; Apr 20th, 2011 at 2:47 am. Reason: Adding code tags to old thread
Reputation Points: 10
Solved Threads: 0
Newbie Poster
serban is offline Offline
3 posts
since Oct 2009
Oct 22nd, 2009
0
Re: How to use set/get ??
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.
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Connecting two computer using C# coding
Next Thread in C# Forum Timeline: TextBox ENTER and ESC sound OnKeyPress





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC