Link to home
Start Free TrialLog in
Avatar of zkeown
zkeownFlag for United States of America

asked on

Alternative to Enum for object oriented key/value pairs?

For the same of code readability, I've created an Enum for Cycle Type:

Public Enum CycleType
 Unknown = 0
 Full = 1
 Partial = 2
End Enum

However, I just got a new requirement that these be configurable via a database -- in other words there will be a table with cycle type ID and description.  Is there a way to populate a [something] from a database while retaining the code readability of an Enum.
ASKER CERTIFIED SOLUTION
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can store the DESCRIPTION of the Enum Value as a String in your Database and then convert it back to the correct Enum Value using code like below.

Note that I changed the Names slightly because VB.Net 2005 Express didn't like "Partial" as a name since it is a reserved keyword...


Public Class Form1
 
    Public Enum CycleType
        UnknownType = 0
        FullType = 1
        PartialType = 2
    End Enum
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dbValue As String = "FullType"
        Dim ct As CycleType = [Enum].Parse(GetType(CycleType), dbValue)
        Debug.Print("dbValue = " & dbValue)
        Debug.Print("ct = " & ct & ", " & ct.ToString)
    End Sub
 
End Class

Open in new window

I am sorry, I thought that you were saying the values could be different based on database (dynamic).  If they are static, I would agree.

Additionally, you can use a class with Public Shared members/properties if that is the case also.