|
-
Feb 3rd, 2011, 09:22 AM
#1
Thread Starter
Fanatic Member
Enum query
If want to create an Enumeration where if I have the number 1,2 or 3 it is enum 1, 3,4 or 5 will be enum 2 and 6 will be enum 3 e.g(psuedo code):
HTML Code:
Enum TestResult
Pass = 1,2 or 3
Maybe = 4 or 5
Screwed = 6
End Enum
or like
Enum TestResult
Pass = 1,2,3
Maybe = 4,5
Screwed = 6
End Enum
How is this implemented?
Thansk In Advance
Last edited by venerable bede; Feb 3rd, 2011 at 09:32 AM.
-
Feb 3rd, 2011, 09:37 AM
#2
Re: Enum query
You can not give the same name to multiple values, how would the compiler known which of the 3 numbers you wanted to select?
If you just want to get the text back, then heres a method you could use. A little long winded, but I use it all over the place:
Code:
Imports System.ComponentModel
Enum MyEnum
<Description("Pass")> Pass1 = 1
<Description("Pass")> Pass2 = 2
<Description("Pass")> Pass3 = 3
<Description("Maybe")> Maybe1 = 4
<Description("Maybe")> Maybe2 = 5
<Description("Screwed")> Screwed = 6
End Enum
Private Sub ShowMe()
MessageBox.Show(GetObjectDescription(MyEnum.Pass1))
MessageBox.Show(GetObjectDescription(MyEnum.Pass3))
MessageBox.Show(GetObjectDescription(MyEnum.Screwed))
End Sub
Private Function GetObjectDescription(ByVal value As Object) As String
Try
Dim fieldInfo As Reflection.FieldInfo = value.GetType().GetField(value.ToString())
Dim attributes() As DescriptionAttribute = DirectCast(fieldInfo.GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())
Return attributes(0).Description
Catch ex As Exception
'Occurs when we attempt to get description of an enum value that does not exist
Try
Return value.ToString
Catch exInner As Exception
Return "Unknown"
End Try
End Try
End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|