Results 1 to 2 of 2

Thread: Dynamically Looping An Enum (ADDED: Using strings as Enum names)

  1. #1

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Dynamically Looping An Enum (ADDED: Using strings as Enum names)

    I just ran across this code in a vbAccelerator.com sample project.

    If you are developing an project and using enums, but find that you are adding things to the Enum often, you can use this cool trick when declaring it:
    VB Code:
    1. Public Enum eMyConstants
    2.    [_First] = 1
    3.    EMCFirstValue = 1
    4.    EMCSecondValue = 2
    5.    EMCThirdValue = 3
    6.    EMCFourthValue = 4
    7.    [_Last] = 4
    8. End Enum

    Then you can use it like this:
    VB Code:
    1. For i = eMyConstants.[_First] To eMyConstants.[_Last]
    2.     ' Do something here
    3. End If

    As you add values to your enum, you don't have to go back and change every loop. All you have to do is change the value of [_Last] and you will be site.

    Note that [_First] and [_Last] don't show up in the Object Brower or any code-tip pop-ups in VB.

    I didn't know this existed until today, so it might function in other areas as well. Who knows. If anyone could give me examples of other ways to use similar syntax, I would appriciate it.

    NOTE: This won't work if your constants are not consecutive. If you all your constants were powers of 2 (a very commen situation), you could do something like this:
    VB Code:
    1. Public Enum eMyConstants
    2.    [_First] = 0
    3.    EMCFirstValue = 1
    4.    EMCSecondValue = 2
    5.    EMCThirdValue = 4
    6.    EMCFourthValue = 8
    7.    [_Last] = 3
    8. End Enum
    9.  
    10. ' In use:
    11. For i = eMyConstants.[_First] To eMyConstants.[_Last]
    12.     lCurrent = 2 ^ i
    13.     ' You should have the current value in the enum at your service
    14.     ' [_Last] Has the value of 3 and 2 ^ 3 is 8 which is our last enum value.
    15. Next i
    Last edited by eyeRmonkey; Dec 8th, 2005 at 07:15 PM.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  2. #2

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Dynamically Looping An Enum (Use when adding things to Enum often)

    Today, while looking through more code written by other people I ran across this other cool Enum trick. It allows you to use strings for your Enum names instead of the normal variable naming setup. It works like this:
    VB Code:
    1. Public Enum eMyEnum
    2.     [My Value Number 1]
    3.     [Another value with a descriptive name here]
    4.     [mwhaha I can use spaces]
    5. End Enum
    6.  
    7. ' In use:
    8. Private Sub Command1_Click()
    9.     Dim eTest As eMyEnum
    10.     ' You get a cool code-sense pop up with all the values like usual,
    11.     ' but this way, you can use spaces and other characters
    12.     a = [Another value with a descriptive name here]
    13.  
    14.     If (a = [My Value Number 1]) Then
    15.         MsgBox "YAY!"
    16.     End If
    17. End Sub

    PS - Thanks to Merri for this one. I saw it in his Sudoku solver code.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

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