Click here to Skip to main content
15,895,667 members
Articles / Programming Languages / Visual Basic

String Enumerations in VB.NET

Rate me:
Please Sign up or sign in to vote.
4.71/5 (4 votes)
9 Nov 2012CPOL1 min read 51.5K   6  
This is an alternative for "String Enumerations in VB.NET"
'Simple Commands via string enumeration by David. Rathbone.
Public Class Form1

    'Enumeration of your programs commands note all in upper case
    Private Enum ProgramCommands
        RUN_MOTOR
        STOP_MOTOR
        MOVE_ARM
        FIRE_GUN
    End Enum

    'Test button to input a command from a textbox
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Get Command From your programs input  
        Dim sCommandToTest As String = TextBox1.Text.ToUpper 'Keep string to upper case
        'First is the command to check in your program commands Enumeration? (True=Yes False= no)
        If CBool(CType([Enum].IsDefined(GetType(ProgramCommands), sCommandToTest), ProgramCommands)) Then
            'Command IS in Enumeration ~ now look though all the commands and get the one selected
            Dim testcase As ProgramCommands = CType([Enum].Parse(GetType(ProgramCommands), sCommandToTest, True), ProgramCommands)
            'Just go through each case to add your commands action
            Select Case testcase
                Case ProgramCommands.RUN_MOTOR
                    Label1.Text = "Run motor action stuff here"
                Case ProgramCommands.STOP_MOTOR
                    Label1.Text = "Stop motor action Stuff here"
                Case ProgramCommands.MOVE_ARM
                    Label1.Text = "Move arm action stuff here"
                Case ProgramCommands.FIRE_GUN
                    Label1.Text = "Fire gun action stuff here"
                Case Else
                    Label1.Text = "Should never get here!"
            End Select
        Else
            'Command Not in Enumeration
            Label1.Text = "False :- No such command"
        End If
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Center Software and Electronics Ltd
United Kingdom United Kingdom
David Rathbone is an electronics and software design engineer and has been in commercial hardware and software development for over 30 years. He has designed a number of products that are now used everyday all over the world.

Comments and Discussions