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

System.Enum Doesn't Have GetValues On Xbox 360?

Last post 14/10/2009 20:18 by UberGeekGames. 17 replies.
  • 03/05/2007 13:42

    System.Enum Doesn't Have GetValues On Xbox 360?

    I just created a new Xbox 360 project for my current game to see if everything compiles fine (almost to a point where I'll be getting the Creator's Club subscription) and I had this problem:

        'System.Enum' does not contain a definition for 'GetValues'

    where the line in question is this:

        values = Enum.GetValues(type);

    The code compiles perfectly on Windows, so I assume this is an issue with the Xbox 360 port of the .NET framework. Is there any work around to doing this yet?

  • 03/05/2007 13:51 In reply to

    Re: System.Enum Doesn't Have GetValues On Xbox 360?

    Not that I know of: this functionality just isn't supported by the Compact Framework.
    XNA Framework Developer - blog - homepage
  • 03/05/2007 14:45 In reply to

    Re: System.Enum Doesn't Have GetValues On Xbox 360?

    Hm. I had one idea, but since I don't have the Creator's Club membership I can't test it. Can someone either test this or take a look and see if I'll have any problems? I tried to fudge a way to get all the values from the Enum I could. The one assumption the method makes about the Enum is that the values start being numbered at 0 and go up by 1 each time. It's not very pretty or efficient, but does anyone have any idea if it will work?

           public class EnumItem : OptionsItem
        {
            public Type Type;
            private int index = 0;

    #if !XBOX
            private Array values;
    #else
            private List<object> values = new List<object>();
    #endif

            public EnumItem(string name, Vector2 position, Type type) : this(name, position, true, type) { }

            public EnumItem(string name, Vector2 position, bool enabled, Type type)
                : base(name, position, enabled)
            {
                Type = type;

                if (!type.IsEnum)
                    throw new Exception("Type must be an Enum");

    #if !XBOX
                values = Enum.GetValues(type);
    #else
                int i = 0;
                object o;
                do
                {
                    try
                    {
                        o = (type)i;
                        values.Add(o);
                        i++;
                    }
                    catch
                    {
                        break;
                    }
                } while(o != null);
    #endif
            }

    ...
    }
  • 03/05/2007 15:10 In reply to

    Re: System.Enum Doesn't Have GetValues On Xbox 360?

    I don't think just casting an integer to an enum will work - that will actually succeed even if the value isn't in the enum. You could probably hack together some kind of check doing a ToString on the resulting enum to see if that gives back anything sensible.

    If this was me, though, I'd just pass in the maximum value as a parameter when constructing my menu.
    XNA Framework Developer - blog - homepage
  • 03/05/2007 22:01 In reply to

    Re: System.Enum Doesn't Have GetValues On Xbox 360?

    Shawn Hargreaves:

    If this was me, though, I'd just pass in the maximum value as a parameter when constructing my menu.


    How would that work? I need to get all the values for the Enum.


    Edit:
    I decided to try my above solution by compiling it for Windows, and it wouldn't compile for Windows. It compiled at least for 360, but when I try compiling it for Windows I get this error:

    The type or namespace name 'type' could not be found (are you missing a using directive or an assembly reference?)

  • 03/05/2007 22:27 In reply to

    Re: System.Enum Doesn't Have GetValues On Xbox 360?

    I wrote a function for this and it works fine on both Windows and XB360 so far.

    (Why the code snippet function in the forum is not work?!)

    ==

    using System.Reflection;

    public static Enum[] GetEnumValues(Type enumType)
    {
      if (enumType.BaseType == typeof(Enum))
      {
        FieldInfo[] info = enumType.GetFields(BindingFlags.Static | BindingFlags.Public);
        Enum[] values = new Enum[info.Length];
        for (int i=0; i<values.Length; ++i)
        {
          values[i] = (Enum)info[i].GetValue(null);
        }
        return values;
      }
      else
      {
         throw new Exception("Given type is not an Enum type");
      }
    }

    ==

    BTW, this function is so useful and I wonder why it is not supported in the .NET CF?

     

  • 03/05/2007 22:33 In reply to

    Re: System.Enum Doesn't Have GetValues On Xbox 360?

    justastro:

    I wrote a function for this and it works fine on both Windows and XB360 so far.

    (Why the code snippet function in the forum is not work?!)

    public static Enum[] GetEnumValues(Type enumType)
    {
      if (enumType.BaseType == typeof(Enum))
      {
        FieldInfo[] info = enumType.GetFields(BindingFlags.Static | BindingFlags.Public);
        Enum[] values = new Enum[info.Length];
        for (int i=0; i<values.Length; ++i)
        {
          values[i] = (Enum)info[i].GetValue(null);
        }
        return values;
      }
      else
      {
         throw new Exception("Given type is not an Enum type");
      }
    }

    BTW, this function is so useful and I wonder why it is not supported in the .NET CF?



    It looks promising, but when I compile I get errors about FieldInfo and BindingFlags. Where are those defined?
  • 03/05/2007 22:40 In reply to

    Re: System.Enum Doesn't Have GetValues On Xbox 360?

    Nick Gravelyn:
    justastro:

    I wrote a function for this and it works fine on both Windows and XB360 so far.

    (Why the code snippet function in the forum is not work?!)

    public static Enum[] GetEnumValues(Type enumType)
    {
      if (enumType.BaseType == typeof(Enum))
      {
        FieldInfo[] info = enumType.GetFields(BindingFlags.Static | BindingFlags.Public);
        Enum[] values = new Enum[info.Length];
        for (int i=0; i<values.Length; ++i)
        {
          values[i] = (Enum)info[i].GetValue(null);
        }
        return values;
      }
      else
      {
         throw new Exception("Given type is not an Enum type");
      }
    }

    BTW, this function is so useful and I wonder why it is not supported in the .NET CF?



    It looks promising, but when I compile I get errors about FieldInfo and BindingFlags. Where are those defined?

    The code needs reflection

    using System.Reflection;

     

  • 03/05/2007 22:42 In reply to

    Re: System.Enum Doesn't Have GetValues On Xbox 360?

    Thanks. Works great on PC and compiles for 360 so I'm going to guess that it works. Thanks a lot.
  • 11/07/2008 1:23 In reply to

    Re: System.Enum Doesn't Have GetValues On Xbox 360?

    A small update to the function. By making it a generic function, I'm able to return the exact type in a List. Also, instead of throwing

    an exception, I just return an empty List.

     

         public static List<T> GetValues<T>()
        {
          Type currentEnum = typeof(T);
          List<T> resultSet = new List<T>();
          if (currentEnum.IsEnum)
          {
            FieldInfo[ fields = currentEnum.GetFields(BindingFlags.Static | BindingFlags.Public);
            foreach (FieldInfo field in fields)
              resultSet.Add((T)field.GetValue(null));
          }

          return resultSet;
        }

  • 14/11/2008 16:04 In reply to

    Re: System.Enum Doesn't Have GetValues On Xbox 360?

    I have also run into need for .GetValues().  I always seem to want more power with my enums than I am allowed to have.

    In my enemy type enum, if I use an index from 0..numEnemyTypes-1, can I used this index to access the type and its string name?

    Basically, I want to do this:

    enemyType tempEnemyType = enemyType[2]  
    string tempStr = enemyType[2].ToString() 

    Where enemyType is an enum of enemy types:

     
    enum enemyType  
    {  
    redEnemy,  
    greenEnemy,  
    ...  
    Matthew Doucette / Xona Games

    ...our upcoming 4-player dual play Xbox 360 2D shooter: Duality ZF (Top 20 in Dream.Build.Play 2009)
  • 14/11/2008 16:54 In reply to

    Re: System.Enum Doesn't Have GetValues On Xbox 360?

    I solved it (my previous post) with type casting (which removes range checking):

    enemyType tempEnemyType = (enemyType)2;  
    string tempStr = ((enemyType)2).ToString(); 

    Is there a better solution?
    Matthew Doucette / Xona Games

    ...our upcoming 4-player dual play Xbox 360 2D shooter: Duality ZF (Top 20 in Dream.Build.Play 2009)
  • 14/11/2008 18:54 In reply to

    Re: System.Enum Doesn't Have GetValues On Xbox 360?

    RocketCanGames:
            FieldInfo[ fields = currentEnum.GetFields(BindingFlags.Static | BindingFlags.Public);


    Just a note to anyone else coming into this thread, that if you do not specify "BindingFlags.Static | BindingFlags.Public", then .GetFields() will return an extra field.  For me, the extra field is: [0] = {Int32 value__}
    Matthew Doucette / Xona Games

    ...our upcoming 4-player dual play Xbox 360 2D shooter: Duality ZF (Top 20 in Dream.Build.Play 2009)
  • 31/03/2009 20:07 In reply to

    Re: System.Enum Doesn't Have GetValues On Xbox 360?

    Thread resurrection go!
    Here's a LINQ version of that method, because it's fun and takes up a single line.

    public static IEnumerable<T> GetValues<T>() 
        return typeof(T).GetFields(BindingFlags.Static | BindingFlags.Public).Select(x => (T)x.GetValue(null)); 

    And I would as well prefer a reference implementation of the method in the Xbox assemblies... I'll make a Connect entry.
  • 31/03/2009 20:18 In reply to

    Re: System.Enum Doesn't Have GetValues On Xbox 360?

    That's not quite LINQ since you're just using the Select extension method. If you want LINQ it'd be like this:

    public static IEnumerable<T> GetValues<T>() 
       return (from x in typeof(T).GetFields(BindingFlags.Static | BindingFlags.Public) 
               select (T)x.GetValue(null)); 

    using that awesome query syntax. :)

    And yeah, I could have put it on one line, but I like breaking up each part of my LINQ queries.
  • 01/04/2009 8:38 In reply to

    Re: System.Enum Doesn't Have GetValues On Xbox 360?

    Actually as far as I understand it your linq query syntax just gets rewritten to his extension method by the compiler. but whatever all the same.

    Good to know about GetValues. The compact framework seems to be restrictive in the most unexpected ways.
  • 01/04/2009 15:44 In reply to

    Re: System.Enum Doesn't Have GetValues On Xbox 360?

    SwiftFalcn:
    Actually as far as I understand it your linq query syntax just gets rewritten to his extension method by the compiler. but whatever all the same.
    It does, but LINQ is the query syntax so if you just use the extension methods it's not technically LINQ. That's all I was pointing out.
  • 14/10/2009 20:18 In reply to

    Re: System.Enum Doesn't Have GetValues On Xbox 360?

    I haven't touched LINQ so using that bit of code as a replacement for Enum.GetValues was more difficult than I expected, so here's a method that actually gets the enum's names and puts them into a string array for you:

    public static string[] GetEnumNames<T>() 
        IEnumerable<T> tx = (from x in typeof(T).GetFields(BindingFlags.Static | BindingFlags.Public) 
                select (T)x.GetValue(null)); 
        List<T> rx = tx.ToList(); 
        List<string> s = new List<string>(); 
        foreach (T t in rx) 
             s.Add(t.ToString()); 
        return s.ToArray(); 

    It's probably not that efficient but I was quickly hacking it together and figured someone else might benefit.
    "No programmer can pick up a TV remote without thinking what it would take to add a stun gun. [...] Their motto is 'if it ain't broke, it doesn't have enough features yet'" - Scott Adams, The Dilbert Principle

    The signature that was too big for the 512 char limit
Page 1 of 1 (18 items) Previous Next