Click here to Skip to main content
13,455,664 members (50,948 online)
Click here to Skip to main content
Add your own
alternative version


5 bookmarked
Posted 13 Aug 2012

String Enumerations in VB.NET

, 9 Nov 2012
Rate this:
Please Sign up or sign in to vote.
This is an alternative for "String Enumerations in VB.NET"


You have a program that needs to parse commands and then based on the result do something?

String enumeration in VB.NET would be the ideal answer, but enumerations in .NET are limited to numbers.

After a lot of web searching for a quick few lines of code, I put this code together to help others as many sites show more complex usage of enumerations as set examples.

The only rule in your program's command strings is keep the string simple without spaces, when you put them into your enumeration.

This little code snippet will then make parsing your command strings simple.

Using the code

Using the code will just require a new VB.NET Windows Forms project with a Textbox (Textbox1) , Label (Label1), and Button (Button1).

All your program command strings are put in the enumeration "ProgramCommands". Running the example will just need you to enter in the input box the exact string as found in your Enum. The label will show if your command was parsed OK.

Note the most important line in the code uses the [Enum].Isdefined function which checks if your string is in the Enum which returns true or false. Then we can parse the test string to see if a match is found. The matching string can then be used in the "Select Case" to perform actions on the command.

'Simple Commands via string enumeration by David. Rathbone.
Public Class Form1

'Enumeration of your programs commands note all in upper case
Private Enum ProgramCommands
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

        'Command Not in Enumeration
        Label1.Text = "False :- No such command"
    End If
End Sub
End Class

Points of Interest

For further details on string enumerations, please see, which will outline more details. In fact the MSN example gave me the idea that a more simple answer was needed!


First version: 13/08/2012.


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


About the Author

My Bones
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.

You may also be interested in...

Comments and Discussions

QuestionA easier method Pin
RossMW29-Apr-14 12:00
memberRossMW29-Apr-14 12:00 
AnswerRe: A easier method Pin
My Bones26-Oct-16 10:37
memberMy Bones26-Oct-16 10:37 
QuestionGreat Example Pin
Stephen Britton12-Feb-14 2:58
memberStephen Britton12-Feb-14 2:58 
SuggestionCan simplify with [Enum].TryParse Pin
Matt T Heffron9-Nov-12 11:16
memberMatt T Heffron9-Nov-12 11:16 
GeneralRe: Can simplify with [Enum].TryParse Pin
My Bones10-Nov-12 0:10
memberMy Bones10-Nov-12 0:10 
QuestionWhy bother with the enum at all? Pin
John Brett14-Aug-12 2:28
memberJohn Brett14-Aug-12 2:28 
AnswerRe: Why bother with the enum at all? Pin
My Bones14-Aug-12 2:44
memberMy Bones14-Aug-12 2:44 
GeneralRe: Why bother with the enum at all? Pin
John Brett14-Aug-12 4:29
memberJohn Brett14-Aug-12 4:29 
GeneralRe: Why bother with the enum at all? Pin
My Bones14-Aug-12 9:28
memberMy Bones14-Aug-12 9:28 
GeneralRe: Why bother with the enum at all? Pin
Mike Tuersley12-Nov-12 7:26
memberMike Tuersley12-Nov-12 7:26 
GeneralRe: Why bother with the enum at all? Pin
My Bones12-Nov-12 7:46
memberMy Bones12-Nov-12 7:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02-2016 | 2.8.180321.1 | Last Updated 9 Nov 2012
Article Copyright 2012 by My Bones
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid