The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Working with Enums

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Darrell Norton

Posts: 876
Nickname: dnorton
Registered: Mar, 2004

Darrell Norton is a consultant for CapTech Ventures.
Working with Enums Posted: May 25, 2005 8:13 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Darrell Norton.
Original Post: Working with Enums
Feed Title: Darrell Norton's Blog
Feed URL: /error.htm?aspxerrorpath=/blogs/darrell.norton/Rss.aspx
Feed Description: Agile Software Development: Scrum, XP, et al with .NET
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Darrell Norton
Latest Posts From Darrell Norton's Blog

Advertisement

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

Read: Working with Enums

Topic: [OT] In response to an email spammer/Scammer Previous Topic   Next Topic Topic: New Avalon, Indigo, WinFX, and Speech API Beta 1 RC

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use