String value for Enum type

As you all know, we have lots of controlled string values in the system. We wish we could define them in the same way we defined  controlled numeric values, Enum.

Since it is not possible as Enum type cannot Inherit from string type.  We settle with something not ideal.

If we really want to make it a controlled list, we make a class full of  constaint values. An example will be  DataEntities.Common.VehicleBodyStyle.

If we take a easy way out, we sacrifice the controllibliblty and define a list of constants in DataEntities.Common namespace and use some kind of prefix for the name of constants. A good example will be IRP Web Service Constants.     

Public Const IRP_MODEL_TYPE_ROAD_TRACTOR As String = "RT"
Public Const IRP_MODEL_TYPE_TRUCK As String = "TK"
Public Const IRP_MODEL_TYPE_TRACTOR As String = "TR"
Public Const IRP_MODEL_TYPE_TRUCK_TRACTOR As String = "TT"
Public Const IRP_MODEL_TYPE_WRECKER As String = "WR"
Public Const IRP_MODEL_TYPE_WRECKER_PLUS As String = "WK"
Public Const IRP_MODEL_TYPE_BUS As String = "BU"
Public Const IRP_MODEL_TYPE_MOBILE_HOME_TOTER As String = "MT"

If we take a closer look at Enum type. Each enum value has a name in string, and a value in numeric ( int16, Int32, or Int64). Some time, if we do not care about the numeric value of the enum value, we just let the compiler take care of it… if we want it to have some string value, we could name the Enum after the string Values we want it to be. However, there are 2 problems with this approach 1) by doing so, we may have to sacrifice some readiblity, 2) what if there are some spaces in the string value we want to us.

Well, I did some research on it and I figured a way to assign a string value to each enum type value in addition of it numical value and the name.

 The solution consits one small class and 2 extended methods.

StringValueAttribute class made it possible to provide a string value for each enum value.


GetEnumValue (of T) method convert a string value to a enum value based on the string value set in its enum definition.

GetStringValue method retrieve the string value from a enum value…

Enough talking, let’s take a look at some code.

This is how we define enum type with String Value:


Public Enum VehicleBodyStyles
        <StringValue("PU")> PickUp
        <StringValue("VA")> Van
        <StringValue("2D")> TwoDoor
        <StringValue("4D")> FourDoor
        <StringValue("AM")> Ambulance
        <StringValue("BU")> Bus
        <StringValue("CN")> Cnvertbl
        <StringValue("DM")> Dump
        <StringValue("HR")> Hearse
        <StringValue("IN")> Incmplt
        <StringValue("LS")> LowSpeed
        <StringValue("MC")> Mtrcycle
        <StringValue("ME")> MotorizedHome
        <StringValue("MX")> Mixer
        <StringValue("PC")> ParkCamper
        <StringValue("PN")> Panel
        <StringValue("RD")> RoadSter
        <StringValue("SW")> StakeOrRack
        <StringValue("BU")> StationWagon
        <StringValue("TC")> TrialerCoach
        <StringValue("TK")> Tank
        <StringValue("TR")> Trailer
        <StringValue("TT")> Tractor
        <StringValue("UT")> Utility
        <StringValue("2W")> TwoWheel
        <StringValue("3W")> ThreeWheel
        <StringValue("MW")> MultipleWheel
        <StringValue("AI")> Air
        <StringValue("TV")> TrackVehicle
        <StringValue("MH")> MobileHome
        <StringValue("PM")> ParkModel
        <StringValue("WR")> TowTruckOrWrecker
        <StringValue("24")> Camper
        <StringValue("MP")> Moped
        <StringValue("SM")> SnowMobile
        <StringValue("RT")> RoadTractor
        <StringValue("WR")> Wrecker
        <StringValue("WK")> WreckerPlus
        <StringValue("MT")> MobileHomeToter
    End Enum


This unit test shows you how to convert string value to enum value and how to retreive string value from a enum value:

<TestMethod()> Public Sub StringValueTest()
   Dim vehicleBodyStyle As VehicleBodyStyles
   Dim myStringValue = "BU"
   vehicleBodyStyle = myStringValue.GetEnumValue(Of VehicleBodyStyles)()   Assert.AreEqual(VehicleBodyStyles.Bus, vehicleBodyStyle)
   Assert.AreEqual(myStringValue, vehicleBodyStyle.GetStringValue())
   Assert.AreEqual(5, DirectCast(vehicleBodyStyle, Int32))
   Assert.AreEqual("Bus", vehicleBodyStyle.ToString())
End Sub

No comments:

Post a Comment