Solved

Alternative to Enum for object oriented key/value pairs?

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Create a form which is copy of a form in vb.net 2 18
cs.Designer Issue(2) 2 21
Long URL in SMS 6 24
Getfiles in vb.net 28 18
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

21 Experts available now in Live!

Get 1:1 Help Now