Click here to Skip to main content
15,886,731 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Getting more from your enumerations

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
11 Dec 2012CPOL1 min read 19.5K   11   5
With LINQ, you can retrieve an enumeration's metadata, such as its minimum and maximum values and whether it contains a given value.

Introduction

I had a situation come up where I needed to test whether a given value in a database fell inside the range of values given by an enumeration. After fussing around with several ways of testing this, I found a simple, elegant solution that seemed worth documenting.

The theory

In the .NET Framework, enumerations are handled by the Enum class. When you have a code such as

C#
public enum SecurityLevel 
{
    Undefined = -1,
    Guest = 0,
    Assistant = 1,
    Representative = 2,
    Employee = 3,
    Executive = 5,
    Administrator = 8
}
VB.NET
VB
Public Enum SecurityLevelEnum
    Undefined = -1
    Guest = 0
    Assistant = 1
    Representative = 2
    Employee = 3
    Executive = 5
    Administrator = 8
End Enum

the Framework actually creates an instance of Enum and populates it with your values. And like any class, Enum has methods that can be invoked.

The practice

Of interest is the Shared (static in C#) method GetValues, which takes the contents of an enumeration type and returns it as an instance of the System.Array class. If we take this result and convert it to an IEnumerable of a given type, we can use the LINQ extentions to get some useful information about the enumeration.

C#
if ( ((int[]) Enum.GetValues(typeof(SecurityLevel))).Contains(7) )
...
if ( 3 >= ((int[]) Enum.GetValues(typeof(SecurityLevel))).Min() )
...
if ( 6 < ((int[]) Enum.GetValues(typeof(SecurityLevel))).Max() )
VB.NET
VB
If CType([Enum].GetValues(SecurityLevel), Integer()).Contains(7) Then
...
If 3 >= CType([Enum].GetValues(SecurityLevel), Integer()).Min() Then
    ...
If 6 < CType([Enum].GetValues(SecurityLevel), Integer()).Max() Then

Note that because Enum is a keyword in VB.NET, using it as a class requires that you use square brackets.

The first example will return False, because the given enumeration does not contain a value that corresponds to the integer value 7. The second and third tests return True.

You can generalize this as a function which will work for any integer-based enumeration:

C#
bool EnumHasValue(Type Typ, int Value)
{
    return ((int[])Enum.GetValues(Typ)).Contains(Value);
}

...
if (EnumHasValue(typeof(SecurityLevel), 7)
VB.NET
VB
Function EnumHasValue(ByVal Typ As Type, ByVal Value As Integer) As Boolean
    Return CType([Enum].GetValues(Typ), Integer()).Contains(Value)
End Function
...
If EnumHasValue(GetType(SecurityLevel), 7) Then

Of course, enumerations do not have to be integer-based: you can convert the result of GetValues into an array of strings, doubles, whatever you can turn into an enumeration.

If your experimentation comes up with something interesting, please post your results as a comment.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Gregory Gadow recently graduated from Central Washington University with a B.S. that combined economics and statistical analysis, and currently works for the Washington Department of Fish & Wildlife as an IT developer. He has been writing code for 30 years in more than a dozen programming languages, including Visual Basic, VB.Net, C++, C#, ASP, HTML, XML, SQL, and R.

Comments and Discussions

 
QuestionUsing Generics Pin
James Curran15-Jun-12 8:27
James Curran15-Jun-12 8:27 
AnswerRe: Using Generics Pin
Gregory Gadow15-Jun-12 9:19
Gregory Gadow15-Jun-12 9:19 
Excellent idea!
AnswerRe: Using Generics Pin
Gregory Gadow15-Jun-12 9:29
Gregory Gadow15-Jun-12 9:29 
SuggestionRe: Using Generics Pin
Matt T Heffron15-Jun-12 10:42
professionalMatt T Heffron15-Jun-12 10:42 
AnswerGetting more from your enumerations Pin
Danelkits8-Jan-15 0:22
Danelkits8-Jan-15 0:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.