[RESOLVED] What does this do...?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: [RESOLVED] What does this do...?

  1. #1
    Join Date
    Dec 2011
    Posts
    5

    [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;

  2. #2
    Join Date
    Jan 2009
    Posts
    596

    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

  3. #3
    Join Date
    Dec 2011
    Posts
    5

    Re: What does this do...?

    Quote Originally Posted by Peter_B View Post
    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
    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?

  4. #4
    Join Date
    Jan 2009
    Posts
    596

    Re: What does this do...?

    Quote Originally Posted by 0026sd View Post
    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.

    Quote Originally Posted by 0026sd View Post
    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.

  5. #5
    Join Date
    Dec 2011
    Posts
    5

    Re: What does this do...?

    Quote Originally Posted by Peter_B View Post
    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!

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,486

    Re: What does this do...?

    Quote Originally Posted by 0026sd View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center