XNA Creators Club Online
Page 1 of 1 (10 items)
Sort Posts: Previous Next

Why is everything an enumerated type?

Last post 5/27/2008 6:29 PM by Sturm. 9 replies.
  • 5/27/2008 5:28 PM

    Why is everything an enumerated type?

    Feel free to ridicule me if this is a stupid question, but why is PlayerIndex an enumerated type rather than just using an integer?  I can see from the debugger that PlayerIndex.One is 0 and PlayerIndex.Four is 3, as I would expect, but it seems to me unwise to assume that this will always be true, since I don't control the code that assigns those values.  Am I wrong?  Can I assume that (int)PlayerIndex.One will always be 0?  If so, why make it an enumerated type?  If not, is there a way to iterate over an enumerated type in C# that I'm not aware of?  I'm beginning to get tired of writing switch statements and if...elseif..elseif.... when a simple array index or for loop would work if this type were an integer.  If I'm simply showing my ignorance (which is quite possible -- I'm still figuring out the differences in C++ and C#), someone please guide me to the light before I pull out what little hair I have remaining.

    </rant>


    Good judgment comes from experience, and experience comes from bad judgment.  -- Barry LePatner

    Sixty years ago I knew everything; now I know nothing; education is a progressive discovery of our own ignorance. -- Will Durant, American philosopher/author [1885-1981]

  • 5/27/2008 5:29 PM In reply to

    Re: Why is everything an enumerated type?

    If you accept an integer in place of that enumeration, what's to stop people from querying player 1874343's controller? Granted you can do (PlayerIndex)1874343 and pass that in, but most people will use the enumeration and, thus, only pass in the accepted values.

    As for looping, you just do the normal for loop and cast:

    for (int i = 0; i < 4; i++) 
          GamePadState gps = GamePad.GetState((PlayerIndex)i); 

  • 5/27/2008 6:01 PM In reply to

    Re: Why is everything an enumerated type?

    Nick Gravelyn:
    If you accept an integer in place of that enumeration, what's to stop people from querying player 1874343's controller?

    An exception would be thrown, just as it is if you do (PlayerIndex)1874343.

    As for looping, you just do the normal for loop and cast:

    I know you can do that, but had believed this to be a bad practice, since there is no guarantee (since the values you are using in your for loop are not documented) that the library's author won't change the values in a later release, thus breaking your code.  I'm probably just excessively paranoid.  :)


    Good judgment comes from experience, and experience comes from bad judgment.  -- Barry LePatner

    Sixty years ago I knew everything; now I know nothing; education is a progressive discovery of our own ignorance. -- Will Durant, American philosopher/author [1885-1981]

  • 5/27/2008 6:04 PM In reply to

    Re: Why is everything an enumerated type?

    Jim Hunter:
    Nick Gravelyn:
    If you accept an integer in place of that enumeration, what's to stop people from querying player 1874343's controller?
    An exception would be thrown, just as it is if you do (PlayerIndex)1874343.
    Correct, but the fact that it takes a PlayerIndex leads most people to look and use one of the four valid PlayerIndex values. There is input guidance by using an enumeration. With an integer, your only guidance is what the compiler allows you to input, which is horrible design in my opinion.

    As for looping, you just do the normal for loop and cast:
    I know you can do that, but had believed this to be a bad practice, since there is no guarantee (since the values you are using in your for loop are not documented) that the library's author won't change the values in a later release, thus breaking your code.  I'm probably just excessively paranoid.  :)
    That is a valid point. There are ways to use the Enum class to get at the values of an enumeration (granted that only works on the desktop CLR right now), but at least in this situation, I'd consider it fairly safe to use the loop. Of course you are totally correct that the values could change, but it's usually more likely that they will stay the same once released.

  • 5/27/2008 6:16 PM In reply to

    Re: Why is everything an enumerated type?

    Jim Hunter:
    is there a way to iterate over an enumerated type in C#
    There is, but I'm not sure it's supported by the compact framework.

    Gotta love that compact framework!
  • 5/27/2008 6:18 PM In reply to

    Re: Why is everything an enumerated type?

    The correct way to perform the desired effect is:

    PlayerIndex index;
    Array players = Enum.GetValues( typeof(PlayerIndex) );
    for (int i = 0; i < players.Length; i++)
    {
        index = (PlayerIndex)players.GetValue(i);
      
        // Perform any per-player actions necessary
    }


    Jeromy Walsh
  • 5/27/2008 6:20 PM In reply to

    Re: Why is everything an enumerated type?

    Correct. But Enum.GetValues does not exist on the .NET Compact Framework, so that method will not work on the Xbox 360 or the Zune; only Windows.
  • 5/27/2008 6:21 PM In reply to

    Re: Why is everything an enumerated type?

    in this case, it's an enum because it is a framework set limitation.  There can only ever be 4 players ... ever.  So an enumeration makes the most sense because it's blatantly obvious to the developer :-)
    Joel Martinez - XNA MVP
    Blog: http://codecube.net
    XNA Unit Testing: Scurvy Test
  • 5/27/2008 6:25 PM In reply to

    Re: Why is everything an enumerated type?


    Nick Gravelyn:
    Correct. But Enum.GetValues does not exist on the .NET Compact Framework, so that method will not work on the Xbox 360 or the Zune; only Windows.

    Aye.  But I can't for the life of me figure out why MS would prefer unsafe casts from Enum to integer than to iterate the values of an Enum in the Compact Framework.  C'est la vie.

    Jeromy Walsh
  • 5/27/2008 6:29 PM In reply to

    Re: Why is everything an enumerated type?

    Or when we go all C#3 and the Xna team get around doing it:

    public static class PlayerIndexExtensions  
    {  
        public static void ForEach(this PlayerIndex playerIndex, PlayerIndexDelegate playerDelegate)  
        {  
            playerDelegate(PlayerIndex.One);  
            playerDelegate(PlayerIndex.Two);  
            playerDelegate(PlayerIndex.Three);  
            playerDelegate(PlayerIndex.Four);  
        }  
    }  
     

    That would simply let you do something like:

    PlayerIndex.ForEach(this.MyPlayerIndexSpecificCode);

    (Or at least something similar)

Page 1 of 1 (10 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG