Click here to Skip to main content
13,594,513 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


10 bookmarked
Posted 15 Jun 2012
Licenced CPOL

Getting more from your enumerations

, 11 Dec 2012
Rate this:
Please Sign up or sign in to vote.
With LINQ, you can retrieve an enumeration's metadata, such as its minimum and maximum values and whether it contains a given value.


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

public enum SecurityLevel 
    Undefined = -1,
    Guest = 0,
    Assistant = 1,
    Representative = 2,
    Employee = 3,
    Executive = 5,
    Administrator = 8
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.

if ( ((int[]) Enum.GetValues(typeof(SecurityLevel))).Contains(7) )
if ( 3 >= ((int[]) Enum.GetValues(typeof(SecurityLevel))).Min() )
if ( 6 < ((int[]) Enum.GetValues(typeof(SecurityLevel))).Max() )
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:

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

if (EnumHasValue(typeof(SecurityLevel), 7)
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.


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


About the Author

Gregory Gadow
Software Developer (Senior)
United States United States
Gregory Gadow is currently a student at Central Washington University, studying for a B.S. in Actuarial Science with plans to graduate in June, 2019. He has been writing code for almost 30 years in more than a dozen programming languages. Previously, he worked for a mid-size brokerage firm as the company's programming department doing VB6, VB.Net, ASP, HTML, XML and SQL. He also held the Series 7 and Series 66 brokerage licenses.

You may also be interested in...


Comments and Discussions

QuestionUsing Generics Pin
James Curran15-Jun-12 8:27
memberJames Curran15-Jun-12 8:27 
AnswerRe: Using Generics Pin
Gregory.Gadow15-Jun-12 9:19
memberGregory.Gadow15-Jun-12 9:19 
AnswerRe: Using Generics Pin
Gregory.Gadow15-Jun-12 9:29
memberGregory.Gadow15-Jun-12 9:29 
SuggestionRe: Using Generics Pin
Matt T Heffron15-Jun-12 10:42
memberMatt T Heffron15-Jun-12 10:42 
AnswerGetting more from your enumerations Pin
Danelkits8-Jan-15 0:22
memberDanelkits8-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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02-2016 | 2.8.180621.3 | Last Updated 11 Dec 2012
Article Copyright 2012 by Gregory Gadow
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid