Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Alternative to Enum for object oriented key/value pairs?

Posted on 2008-10-06
3
Medium Priority
?
1,880 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:zkeown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 22653878
There is a generic object in .NET called KeyValuePair(Of T, T) that you can use in your instance as KeyValuePair(Of String, Integer).

You would use String == Description and Integer == ID.

http://msdn.microsoft.com/en-us/library/5tbh8a42.aspx
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22654063
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

0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22654783
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.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question