-
December 19th, 2011, 09:21 AM
#1
[RESOLVED] What does this do...?
Hello!
I've come into some source code that I'm picking apart and I'm wondering what this piece of code does:
Code:
public Player Player
{
get { return player; }
}
Player player;
// Here's another example:
public int Score
{
get { return score; }
}
int score;
-
December 19th, 2011, 09:45 AM
#2
Re: What does this do...?
The short answer - that code is defining 'get' accessors for a couple of properties of whatever class those methods are in.
The long answer - http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx
-
December 19th, 2011, 09:49 AM
#3
Re: What does this do...?
Originally Posted by Peter_B
So, this would replace the get and set methods that I would normally put within the class?
Ok, here's a new question. Is this one way of accessing properties from another class without actually creating a new instance of that class?
-
December 19th, 2011, 10:02 AM
#4
Re: What does this do...?
Originally Posted by 0026sd
So, this would replace the get and set methods that I would normally put within the class?
Yes, it is just a bit of syntactic sugar so you can write your code like, e.g.:
Code:
a.X = 5; <-- uses 'setter'
b = a.X;<-- uses 'getter'
instead of
Code:
a.SetX(5);
b = a.GetX();
It is useful when you start off with a class in which some of the data members are directly exposed, but then you realise (maybe through changing requirements) that some validation is needed. Then you can just define the setter and getter methods but don't need to change the code which accesses the data members.
Originally Posted by 0026sd
Ok, here's a new question. Is this one way of accessing properties from another class without actually creating a new instance of that class?
No, it is purely a different syntax to do what you would previously have done using GetX(), SetX() methods.
-
December 19th, 2011, 10:12 AM
#5
Re: What does this do...?
Originally Posted by Peter_B
Yes, it is just a bit of syntactic sugar so you can write your code like, e.g.:
Code:
a.X = 5; <-- uses 'setter'
b = a.X;<-- uses 'getter'
instead of
Code:
a.SetX(5);
b = a.GetX();
It is useful when you start off with a class in which some of the data members are directly exposed, but then you realise (maybe through changing requirements) that some validation is needed. Then you can just define the setter and getter methods but don't need to change the code which accesses the data members.
No, it is purely a different syntax to do what you would previously have done using GetX(), SetX() methods.
Perfect!
Thanks so much for you help!
-
December 21st, 2011, 06:47 PM
#6
Re: What does this do...?
Originally Posted by 0026sd
Is this one way of accessing properties from another class without actually creating a new instance of that class?
As Peter pointed out, not really, but understand that you can define a property as static. There are also the concept of auto-properties.
Let's look at a contrived example of a class that holds a static Timeout value.
Code:
class static Utility
{
public static Utility( )
{
Timeout = 5000; // uses the private setter for the Timeout property
}
public static int Timeout { get; private set; } //auto property with private setter
}
To use the class, we invoke it using the static syntax:
Code:
int timeout = Utility.Timeout; // timeout is set to 5000 using the public Timeout getter
Of course, in the example above you would probably use a const int value for Timeout rather than a static int.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|