CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Darrell Norton's Blog [MVP]

Fill in description here...

Working with Enums

Geoff wrote a whole bunch of code for dealing with Enums. Enums are annoying because you can cast any integer to an Enum type and you will not get an exception message (at least in version 1.0 and 1.1). Geoff's solution uses the IsDefined static method on Enum, but using the IsDefined method really sucks.

If I had a Flags enum, I'd write something like this (the one assumption I make is that the Flags enum is written with values in ascending order):

.cf { font-family: Lucida Console; font-size: 10pt; color: black; background: white; border-top: windowtext 1pt solid; padding-top: 0pt; border-left: windowtext 1pt solid; padding-left: 0pt; border-right: windowtext 1pt solid; padding-right: 0pt; border-bottom: windowtext 1pt solid; padding-bottom: 0pt; } .cln { color: teal; } .cb1 { color: blue; } .cb2 { color: maroon; }
 1 Module Module1
 2  
 3   Sub Main()
 4     For i As Integer = 1 To 20
 5       Console.WriteLine("Number: " + i.ToString() + "  " + IsValid(i).ToString())
 6     Next
 7   End Sub
 8  
 9   <Flags()> _
10   Public Enum MyEnum
11     Val0 = 0
12     Val1 = 1
13     Val2 = 2
14     Val3 = 4
15     Val4 = 8
16   End Enum
17  
18   Public Function IsValid(ByVal val As Integer) As Boolean
19     Dim values As Integer() = System.Enum.GetValues(GetType(MyEnum))
20     Return val < (values(values.GetLength(0) - 1) * 2)
21   End Function
22  
23 End Module

Published May 25 2005, 07:37 AM by darrell
Filed under:

Comments

Sahil Malik said:

Happy 600 !! :-D
# May 25, 2005 9:57 AM

darrell said:

Thanks Sahil! I hadn't even noticed. :)
# May 26, 2005 10:17 AM
Check out Devlicio.us!