Results 1 to 4 of 4
  1. #1
    Join Date
    May 2004
    Location
    UK
    Posts
    709

    C# Properties - Can they call a Method?

    Hey,

    Just wondering, can a property call on a method?

    Lets say for example I do this:

    Game.State = GameState.MainMenuState...

    note Game.State is a property... lets say the field and property looks like this:

    Code:
    private State state;
    
    public State State
    {
         get { return state; }
         set
         {
              // call appropriate method(s)
              
              // set state to value
              state = value;
         }
    }


    Ok the reason I want to do something like this is because every state has an associated screen (main menu, gameplay mode 1, gameplay mode 2, options, leaderboard, and credits)... what would be nice is if when the gamestate is changed, it automatically calls on the methods that will tell which screen to show and which one to hide... Each show/hide method for each screen can be overridden so that for example, my main menu flies into and out of the screen from all angles when it's told to show/hide. The leaderboard might just appear in a flash for example...

    Basically, changing the game's overall state needs to trigger the show/hide automatically, or at least i would like it to... In theory this should all work out nicely, but does C# support method calls from within the accessors of a property?

    Cheers

    Mat
    Last edited by Mat_Brummitt; 01-30-2010 at 06:44 AM.

  2. #2
    Join Date
    May 2004
    Location
    UK
    Posts
    709
    Alternative, I could have a method for setting state, rather than use a property directly to set it, which would then allow me to call on other methods and whatever... however, i remember you saying Buzz, that properties are the new way of doing things, instead of using accessor methods like SetThis or SetThat:

    In this method, I would
    1) pass in the state I want to change to
    2) set the existing state's screen to be invisible
    3) set the state to the new one i passed in
    4) set the new state's screen to be visible

    public void SetState(State state)
    {
    Game.State.Screen.Visible = false;
    Game.State = state;
    Game.State.Screen.Visible = true;
    }
    Would this be the best way to go about this, or should I do something similar to my original post?

    Thanks

    Mat

  3. #3
    Join Date
    Dec 2002
    Location
    Virginia Beach, VA
    Posts
    861
    Yes you can call a method from inside of a property. I use a similar technique to your first example except I only call one method from inside the property and I call the method ChangeState().

    Code:
    public State State
    {
        get { return state; }
        set
        {
            if (state != value)
            {
                 state = value;
                 ChangeState();
            }
        }
    }
    Last edited by chronos78; 01-30-2010 at 01:54 PM.

  4. #4
    Join Date
    May 2004
    Location
    UK
    Posts
    709
    Awesome!

    That's great to know!

    Cheers for the help.

    Mat

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •