Solved

Convert Enum to/from string

Posted on 2008-10-29
4
994 Views
Last Modified: 2010-05-25
I need to be able to do two things, convert a string value to the enum integer
an
convert an integer to the enum string value

e.g.


Public Enum MyEnum
  A = 1
  B = 2
  C = 3
  D = 4
  E = 5
  F = 6
End Enum
 
Dim columnInt as integer = ConvertToInteger("B") 'returns 2
Dim columnStr as string = ConvertToString(2+3) 'returns "E"

Open in new window

0
Comment
Question by:raterus
[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
4 Comments
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22831870
You will have to write switch case statement inside the functions to return appropriate values
0
 
LVL 33

Accepted Solution

by:
raterus earned 0 total points
ID: 22831907
I figured it out,

'To String
[Enum].Getname(GetType(MyEnum), 3)

'From String to Enum
[Enum].Parse(gettype(MyEnum), "C")
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 22832002
Write the code for ConvertToInteger and ConvertToString with Select Case

However, the thing I usually like to do is define the functions to convert ENum to String and String to Enum and forget about the actual underlaying values.


Public Enum MyEnum
  MyEnum_Invalid = 0
  A = 1
  B = 2
  C = 3
  D = 4
  E = 5
  F = 6
End Enum
 
Public Function MyEnumToString( Value as MyEnum ) as String
    Select Case Value
        Case A: MyEnumToString = "A"
        Case B: MyEnumToString = "B"
        Case C: MyEnumToString = "C"
        Case D: MyEnumToString = "D"
        Case E: MyEnumToString = "E"
        Case F: MyEnumToString = "F"
        Case Else: MyEnumToString = ""
    End Select
End Function
 
Public Function StringToMyEnum( Value as String ) as MyEnum
    Select Case Value
        Case "A": StringToMyEnum = A
        Case "B": StringToMyEnum = B
        Case "C": StringToMyEnum = C
        Case "D": StringToMyEnum = D
        Case "E": StringToMyEnum = E
        Case "F": StringToMyEnum = F
        Case Else: StringToMyEnum = MyEnum_Invalid
    End Select
End Function
 
*** If you want to stick with Integers and Strings, then try this ***
 
Public Function ConvertToInteger( Value as string ) as Integer
    Select Case Left( Value,1 )
        Case "A": ConvertToInteger = 1
        Case "B": ConvertToInteger = 2
        Case "C": ConvertToInteger = 3
        Case "D": ConvertToInteger = 4
        Case "E": ConvertToInteger = 5
        Case "F": ConvertToInteger = 6
        Case Default: ConvertToInteger = 0
    End Select
End Function
 
Public Function ConvertToString( Value as Integer ) as String
    Select Case Value
        Case 1: ConvertToString = "A"
        Case 2: ConvertToString = "B"
        Case 3: ConvertToString = "C"
        Case 4: ConvertToString = "D"
        Case 5: ConvertToString = "D"
        Case 6: ConvertToString = "F"
        Case Default: ConvertToString = ""
    End Select
End Function

Open in new window

0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22832024
Thats good one. I did not know that before. Thank you raterus
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

726 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