Solved

Alternative to Enum for object oriented key/value pairs?

Posted on 2008-10-06
3
1,831 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
  • 2
3 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 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 85

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 59

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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