Solved

Alternative to Enum for object oriented key/value pairs?

Posted on 2008-10-06
3
1,814 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Not showing page correctly 3 31
Run time Error 4 34
Device Location 4 13
I need help converting a bitmap to an image in VB.Net 1 15
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now