[C#: Windows] enum uhh enuming?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #1

    [C#: Windows] enum uhh enuming?

    I saw it referenced in a post here and now I'm kicking myself for not marking it.
    Is there a way to enumerate through your enums?

    Supose you have:
    Code:
    public enum PeriphType
        {
            PTYPE_ERROR,
            PTYPE_NONE,
            PTYPE_HOST,
            PTYPE_TEMPHUM,
            PTYPE_DUST,
            PTYPE_AIRPRESS,
            PTYPE_DCVOLTS
        };
    I know I can do this:
    Code:
    string mys=PeriphType.PTYPE_NONE.ToString();
    //mys is now ="PTYPE_NONE"
    I'm looking for something along the lines of:
    Code:
    foreach(<something> i in <auto-collection of all the PerhipType enums>)
    {
       string mys=i.ToString();
    }
    I'm really mad I can't remember it. Something to do with current assemblies? I dunno.

    Any ideas?
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    Hi Plater,
    you got me with this one !!
    I remember doing something like this a few weeks ago, unfortunately had to scrap it.

    this iterates the values
    [CODE=cpp]foreach (PerhipType ptype in System.Enum.Get Values(typeof(P erhipType )))
    {
    //code is here
    }[/CODE]
    this iterates the names
    [CODE=cpp]foreach (string str in System.Enum.Get Names(typeof(Pe rhipType )))
    {
    //code is here
    }[/CODE]

    I think it is like that !!!

    Comment

    • balabaster
      Recognized Expert Contributor
      • Mar 2007
      • 798

      #3
      Originally posted by Shashi Sadasivan
      Hi Plater,
      you got me with this one !!
      I remember doing something like this a few weeks ago, unfortunately had to scrap it.

      this iterates the values
      [CODE=cpp]foreach (PerhipType ptype in System.Enum.Get Values(typeof(P erhipType )))
      {
      //code is here
      }[/CODE]
      this iterates the names
      [CODE=cpp]foreach (string str in System.Enum.Get Names(typeof(Pe rhipType )))
      {
      //code is here
      }[/CODE]

      I think it is like that !!!
      This is how I have it in one of my projects...with names changed to protect those otherwise incriminated ;)

      VB for you because I know how you hate that :oP
      Code:
      Dim MyEnumItems() As MyEnum = System.Enum.GetValues(GetType(MyEnum))
      For Each EnumItem As MyEnum In MyEnumItems 
        Console.WriteLine(EnumItem & ": " & CType(EnumItem, Integer))
      Next
      C#
      Code:
      MyEnum[] MyEnumItems = (MyEnum[])System.Enum.GetValues(typeof(MyEnum));
      foreach (MyEnum EnumItem in MyEnumItems){
        Console.WriteLine(EnumItem+": "+(int)EnumItem);
      }
      ...and GetNames just gets the string names and GetValues actually gets the enumeration item as opposed to just the value which means you have access to both the value and the string.

      If you use GetValues and you only require the value, cast it as an integer, otherwise by reference to the EnumItem you get the string name.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Thanks, I can't even remember why I wanted it, it's been so long (I suspect it was from when I was coding an smtp client).
        I was really kicking myself for not knowing how simple it was.

        Edit: for the immediate time, it doesn't help me since it's not available in the compact framework

        Comment

        Working...