?
Solved

Alternative to Enum for object oriented key/value pairs?

Posted on 2008-10-06
3
Medium Priority
?
1,861 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

762 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