How to loop each items in enum ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cyshao

    How to loop each items in enum ?

    Hi ,my friends:

    I have a enum type ,EMyEnum , defigned as [Flag()].
    I just want to do something like this:

    foreach( EMyEnum crrEnum in EMyEnum )
    {
    //do something
    }

    ////////////////////////////////////////////////////
    But I can't find any way to do that, and only used a very foolish method
    like this

    string[] names = Enum.GetNames(t ypeof(EMyEnum ));
    foreach (string crrEnumName in names )
    {
    EMyEnum crrEnum = (EMyEnum )Enum.Parse( typeof(EMyEnum ) ,
    crrEnumName );
    //do something
    }

    ///////////////////////////////////////////////////
    Are there any good ideas to do that?
    Or Microsoft forget to design a method like this:
    vector< EMyEnum > Enum.GetTypes( typeof(EMyEnum ) )

    Thanks

    CYShao ^_^







  • Peter Rilling

    #2
    Re: How to loop each items in enum ?

    Try the Enum.GetNames() method.

    "cyshao" <adadad@263.net > wrote in message
    news:e6LkZ0QKFH A.1476@TK2MSFTN GP09.phx.gbl...[color=blue]
    > Hi ,my friends:
    >
    > I have a enum type ,EMyEnum , defigned as [Flag()].
    > I just want to do something like this:
    >
    > foreach( EMyEnum crrEnum in EMyEnum )
    > {
    > //do something
    > }
    >
    > ////////////////////////////////////////////////////
    > But I can't find any way to do that, and only used a very foolish method
    > like this
    >
    > string[] names = Enum.GetNames(t ypeof(EMyEnum ));
    > foreach (string crrEnumName in names )
    > {
    > EMyEnum crrEnum = (EMyEnum )Enum.Parse( typeof(EMyEnum ) ,
    > crrEnumName );
    > //do something
    > }
    >
    > ///////////////////////////////////////////////////
    > Are there any good ideas to do that?
    > Or Microsoft forget to design a method like this:
    > vector< EMyEnum > Enum.GetTypes( typeof(EMyEnum ) )
    >
    > Thanks
    >
    > CYShao ^_^
    >
    >
    >
    >
    >
    >
    >[/color]


    Comment

    • Alexander Shirshov

      #3
      Re: How to loop each items in enum ?

      Well, if your method is foolish then I've been posing as a fool hundreds of
      times! :-)

      I routinely use Enum.GetNames to fill a combobox for instance and then
      Enum.GetValues to get the selected value. Did you look at GetValues method?
      Your sample could be rewritten as:

      foreach (EMyEnum val in Enum.GetValues( typeof(EMyEnum) ))
      {
      Console.WriteLi ne(val);
      }


      And then there's Reflection of course. Although an overkill in most
      enum-related tasks, it allows some neat tricks like working with custom
      attributes:

      class FriendlyNameAtt ribute : Attribute
      {
      public readonly string Value;
      public FriendlyNameAtt ribute(string value)
      {
      Value = value;
      }
      }

      enum MyEnum {
      [FriendlyName("V alue of One")]
      One,
      [FriendlyName("V alue of Two")]
      Two,
      Three
      };

      class MainClass
      {
      public static void Main(String[] args)
      {
      foreach (FieldInfo fi in typeof(MyEnum). GetFields())
      {
      FriendlyNameAtt ribute[] names =
      (FriendlyNameAt tribute[])fi.GetCustomAt tributes(typeof (FriendlyNameAt tribute),
      true);
      if (names.Length > 0)
      {
      Console.WriteLi ne(names[0].Value);
      }
      else
      {
      Console.WriteLi ne(fi.Name);
      }
      }
      }
      }


      HTH,
      Alexander


      "cyshao" <adadad@263.net > wrote in message
      news:e6LkZ0QKFH A.1476@TK2MSFTN GP09.phx.gbl...[color=blue]
      > Hi ,my friends:
      >
      > I have a enum type ,EMyEnum , defigned as [Flag()].
      > I just want to do something like this:
      >
      > foreach( EMyEnum crrEnum in EMyEnum )
      > {
      > //do something
      > }
      >
      > ////////////////////////////////////////////////////
      > But I can't find any way to do that, and only used a very foolish method
      > like this
      >
      > string[] names = Enum.GetNames(t ypeof(EMyEnum ));
      > foreach (string crrEnumName in names )
      > {
      > EMyEnum crrEnum = (EMyEnum )Enum.Parse( typeof(EMyEnum ) ,
      > crrEnumName );
      > //do something
      > }
      >
      > ///////////////////////////////////////////////////
      > Are there any good ideas to do that?
      > Or Microsoft forget to design a method like this:
      > vector< EMyEnum > Enum.GetTypes( typeof(EMyEnum ) )
      >
      > Thanks
      >
      > CYShao ^_^
      >
      >
      >
      >
      >
      >
      >[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: How to loop each items in enum ?

        Alexander Shirshov <alexander@omni talented.com> wrote:[color=blue]
        > Well, if your method is foolish then I've been posing as a fool hundreds of
        > times! :-)[/color]

        I don't think so...
        [color=blue]
        > I routinely use Enum.GetNames to fill a combobox for instance and then
        > Enum.GetValues to get the selected value. Did you look at GetValues method?
        >
        > Your sample could be rewritten as:
        >
        > foreach (EMyEnum val in Enum.GetValues( typeof(EMyEnum) ))
        > {
        > Console.WriteLi ne(val);
        > }[/color]

        And that's the difference - I'd expect that calling GetValues is much
        cheaper than calling GetNames and then calling Enum.Parse on each name.

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        Working...