Results 1 to 7 of 7

Thread: [RESOLVED] [2.0] Iterating through an enumeration's possible values

  1. #1

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Resolved [RESOLVED] [2.0] Iterating through an enumeration's possible values

    I've got a Type variable that is an enumeration, although i don't know which one. Is there an easy of listing it's possible values, along with their names?
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  2. #2
    Hyperactive Member ahmad_iam's Avatar
    Join Date
    Jun 2005
    Location
    Pakistan
    Posts
    265

    Re: [2.0] Iterating through an enumeration's possible values

    Quote Originally Posted by SLH
    I've got a Type variable that is an enumeration, although i don't know which one. Is there an easy of listing it's possible values, along with their names?
    If we have a ennumeration variable, then we can easily show its possible values by writing the name of the enumeration variable and after that place dot(.) you will see it's possible values of that enumeration type varialbe.


    Imran Ahmad Mughal
    "Visual Studio .NET 2005/.NET Framework 2.0"

    The eyes are the greatest telescopes, one will ever need to study the universe. Let the heart be the tripod, the mind be the shaft, then let the eyes become the lens of that great telescope of which God gave to us all.

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [2.0] Iterating through an enumeration's possible values

    Quote Originally Posted by ahmad_iam
    If we have a ennumeration variable, then we can easily show its possible values by writing the name of the enumeration variable and after that place dot(.) you will see it's possible values of that enumeration type varialbe.


    Imran Ahmad Mughal
    That will give you the name of each enumeration... cast it to integer to get the values
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Iterating through an enumeration's possible values

    Code:
    foreach (string name in Enum.GetNames(typeof(MessageBoxButtons)))
    {
        MessageBox.Show("Name: " + name);
    }
    foreach (MessageBoxButtons value in Enum.GetValues(typeof(MessageBoxButtons)))
    {
        MessageBox.Show("Value (as string): " + value.ToString());
        MessageBox.Show("Value (as int): " + (int)value);
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: [2.0] Iterating through an enumeration's possible values

    Thanks for the example, but what type is Enum?

    I have my Type variable (which i know is an enum) which doesn't have a GetNames or a GetValues method.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Iterating through an enumeration's possible values

    Enum IS the type. GetNames and GetValues are static methods of the Enum class. Notice that you pass the Type of your specific enumerated type to the method as a parameter. I've just used MessageBoxButtons as an example. You can pass your own enumerated Type instead.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: [2.0] Iterating through an enumeration's possible values

    Ah ha, gottya. Didn't realise MessageBoxButtons was an enumeration!

    Thanks for your help.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


Posting Permissions

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



Click Here to Expand Forum to Full Width