How to initialize an enum from a string or int value in c#

I didn’t know you could do this, but I tried and it seems fine so it’s worth sharing.

I’m working in a large code base, where my input might come from a web form, or an imported file, or even be passed in to a WCF service. I didn’t write this originally, but I need to bolster the validation of one of the input fields, which involves doing different validations depending on the type of the thing passed in. By “type”, I refer not to the .NET type but the kind of thing it is. For example, if it were an account it could be a cheque or savings account; if it were an ID it could be a local or international passport or a local ID document or birth certificate number, and so on. We already have the type, but it wasn’t being used.

My problem is, the type is sometimes an integer (hard-coded by my predecessor – for example hard-coded in the markup of a combobox control and it is the ID of the thing type) and other times a string. While I could change all the code everywhere to look up the ID in the database, that would involve a lot of work, such as changing the code that hard-codes values to bind to the database properly… and so on.

What I need then, is to be able to initialize an enum in all of those places, using either:

  1. A string literal.
  2. An integer that represents the value of the enum.

It turns out to be easy. For an integer, I can cast directly to the enum type (in other words, an explicit conversion), and for a string, I use the Enum.Parse method.

Here is a simplified code example that demonstrates how easy it is. This code is only intended to demonstrate how easy the initialization of the enum from a correct value or string is. Note that in my real code, I am not blindly casting a user input field into an enum. I’m using the enum that represents a type of item, which I know to be valid, to validate a second field.

using System;

namespace Demo
    enum Thing : int
        Nothing = 0,
        Gizmo = 1,
        Doodad = 2,
        Thingamajig = 3,
        Whatchamacallit = 4

    public static class Example
        public static bool ValidateStringThing(string thing)
            Thing value = (Thing)Enum.Parse(typeof(Thing), thing);
            return ValidateThing(value);

        public static bool ValidateIntThing(int thing)
            Thing value = (Thing)thing;
            return ValidateThing(value);

        private static bool ValidateThing(Thing thing)
            switch (thing)
                case Thing.Nothing:
                    return false;
                case Thing.Gizmo:
                case Thing.Doodad:
                case Thing.Thingamajig:
                case Thing.Whatchamacallit:
                    return true;
                    return false;

About Jerome

I am a senior C# developer in Johannesburg, South Africa. I am also a recovering addict, who spent nearly eight years using methamphetamine. I write on my recovery blog about my lessons learned and sometimes give advice to others who have made similar mistakes, often from my viewpoint as an atheist, and I also write some C# programming articles on my programming blog.
This entry was posted in Programming and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s