idodev rss feed

Iterating through an Enum in VB.NET

Just a quick one, while I remember. How to loop (for each) through all values in an enum in VB.NET

For demonstration purposes, lets say we have an Enum of image sizes that we need to look through.

The Enum

Public Enum ImageSizeEnum
    Thumb = 0
    Small = 1
    Medium = 2
    Large = 3

Iterating the Enum

Dim EnumSizeValues As Array = System.[Enum].GetValues(GetType(ImageSizeEnum))
For Each Size As ImageSizeEnum In EnumSizeValues
    'Do something clever with Size
    Console.WriteLine("Size: {0}", Size)

Awesome! I hit a need to do this and figured it would be a real pain. In the end this was a fairly elegant and simple solution.

comments powered by Disqus
Toby Foord - Web Developer

Hi, I'm Toby Foord. A 28 year old developer, wannabe designer, husband, and dad. I ocassionally commit, once in a while I tweet, and on a blue moon I might chat.