Click here to Skip to main content
11,720,324 members (80,819 online)
Click here to Skip to main content

Tagged as

C# Enum with Char Valued Items

, 3 May 2010 CPOL 44.3K 8
Rate this:
Please Sign up or sign in to vote.
How to use a char as a value for enum items, and why

Introduction

Focusing on the subject, what am I going to write about the Enum type?

  • Can we assign an Enum item to anything that's not of the int type?
  • Why would I even bother thinking about this?
  • How can I handle converting something to Enum and back to something again?

Background

This all started when I had a database field with a char, and needed to have a strongly typed way of handling it in my C# code, and then I figured it out, it's possible.

Using the Code

First of all, YES, we can assign an Enum to something else, a char!
And how?
Just like you're thinking about it, yes:

public Enum myEnum
{
  value1 = 'a',
  value2 = 'b'
}  

And why would I think about this?

Have you ever had to write some DAL code or mapping an existing database to an ORM, say Entity Framework, and you had fields which contained, for some particular reason, a list of controlled chars, which aren't related to any other table, like a field called State which had the possible values, 'R' for Ready, 'P' for Pending and 'C' for Cancelled, and you want to have some nice and type-safe way of manipulating this field on code and at the same time a generic way, which can be used everywhere, well, you can do it using those chars on an Enum type:

public Enum State
{
  Ready = 'R',
  Pending = 'P',
  Cancelled = 'C'
} 

And how can I handle this? I'll sure try to do a State.ToString() and it will return a number to me, why??

Because, actually, you can't have an Enum item with an associated char, but .NET lets you do this, and internally it converts your char to its equivalent int representation (ASCII).

So, now you're thinking, so now what? How can I get my char??
Simple enough, just cast the Enum item to a char first, and then you get its char representation:

string type = ((char)StateEnum).ToString();  

This way, you can extract the char from the int, and get your value!
This is for persisting your data to your datasource (convert the Enum item to the value that your datasource is expecting, the char).

But now, you need to convert your char to the corresponding Enum item, when you get your char field from your datasource, right?
How can this be done?
Well I've coded a method to do that, with generics, let's see:

Code at pastebin

public static T ToEnum< T >(string @string)   
{
   if (string.IsNullOrEmpty(@string))
   {
    throw new ArgumentException("Argument null or empty");
   }
   if (@string.Length > 1)
   {
    throw new ArgumentException("Argument length greater than one");
   }
   return (T)Enum.ToObject(typeof(T), @string[0]);
} 

So what you do here is accept a string (could be a char, it's just to make it simpler, since many ORMs map a char on the database to string, not char), and then you check your parameters, if they're not null, and if the length of the string is one (which matches a char), and then, you use the ToObject method from the Enum type, that accepts the return type you want to get, and the corresponding Enum item value (int or char) to convert to.

And that's it, you can use chars with an Enum object, isn't this awesome? When I got around this, I just thought about the numerous times that I needed it...

Hope this helps you as much as it has helped me.

Points of Interest

The key discovery was the conversion from the enum to char, you need to cast it to char first, and only then you can have your char value, because since the .NET Framework thinks it's an int, you must convert it to char (so it translates the int to the equivalent char on the ASCII table, like the old method chr()).

History

  • 3rd May, 2010: Initial post

License

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

Share

About the Author

ricrodrigues
Software Developer (Senior) Truphone
Portugal Portugal
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 2 Pin
nguyen1908875-Jun-12 21:23
membernguyen1908875-Jun-12 21:23 
AnswerOne way to use Pin
Anamotiris4-May-10 22:24
memberAnamotiris4-May-10 22:24 
GeneralRe: One way to use Pin
ricrodrigues4-May-10 22:37
memberricrodrigues4-May-10 22:37 
GeneralMy vote of 2 Pin
grif4-May-10 4:21
membergrif4-May-10 4:21 
GeneralRe: My vote of 2 Pin
ricrodrigues4-May-10 4:50
memberricrodrigues4-May-10 4:50 
GeneralStrange Pin
Juicy3-May-10 18:46
memberJuicy3-May-10 18:46 
GeneralRe: Strange Pin
ricrodrigues4-May-10 2:58
memberricrodrigues4-May-10 2:58 
GeneralRe: Strange Pin
PIEBALDconsult4-May-10 3:00
mvpPIEBALDconsult4-May-10 3:00 
GeneralRe: Strange Pin
ricrodrigues4-May-10 3:16
memberricrodrigues4-May-10 3:16 
GeneralMore thoughts Pin
PIEBALDconsult3-May-10 15:41
mvpPIEBALDconsult3-May-10 15:41 
GeneralRe: More thoughts Pin
ricrodrigues4-May-10 2:59
memberricrodrigues4-May-10 2:59 
GeneralRe: More thoughts Pin
PIEBALDconsult4-May-10 3:45
mvpPIEBALDconsult4-May-10 3:45 
GeneralRe: More thoughts Pin
ricrodrigues4-May-10 3:55
memberricrodrigues4-May-10 3:55 
GeneralRe: More thoughts Pin
PIEBALDconsult4-May-10 3:59
mvpPIEBALDconsult4-May-10 3:59 
GeneralRe: More thoughts Pin
ricrodrigues4-May-10 4:23
memberricrodrigues4-May-10 4:23 
GeneralRe: More thoughts Pin
PIEBALDconsult4-May-10 11:03
mvpPIEBALDconsult4-May-10 11:03 
It does if you provide an XML comment:

/** <summary>Ready</summary> */ R = 'R'
GeneralRe: More thoughts Pin
ricrodrigues4-May-10 11:33
memberricrodrigues4-May-10 11:33 
GeneralRe: More thoughts Pin
PIEBALDconsult4-May-10 12:47
mvpPIEBALDconsult4-May-10 12:47 
GeneralRe: More thoughts Pin
ricrodrigues4-May-10 12:55
memberricrodrigues4-May-10 12:55 
GeneralThoughts Pin
PIEBALDconsult3-May-10 4:58
mvpPIEBALDconsult3-May-10 4:58 
GeneralRe: Thoughts Pin
ricrodrigues3-May-10 7:15
memberricrodrigues3-May-10 7:15 
GeneralRe: Thoughts Pin
PIEBALDconsult3-May-10 12:57
mvpPIEBALDconsult3-May-10 12:57 
GeneralRe: Thoughts Pin
ricrodrigues3-May-10 14:16
memberricrodrigues3-May-10 14:16 
GeneralAlternate solution Pin
derekschwechel3-May-10 3:54
memberderekschwechel3-May-10 3:54 
GeneralRe: Alternate solution Pin
ricrodrigues3-May-10 4:28
memberricrodrigues3-May-10 4:28 
GeneralRe: Alternate solution Pin
derekschwechel3-May-10 15:27
memberderekschwechel3-May-10 15:27 
GeneralRe: Alternate solution Pin
ricrodrigues4-May-10 3:14
memberricrodrigues4-May-10 3:14 
GeneralRe: Alternate solution Pin
dzCepheus4-May-10 4:34
memberdzCepheus4-May-10 4:34 
GeneralRe: Alternate solution Pin
ricrodrigues4-May-10 4:47
memberricrodrigues4-May-10 4:47 
GeneralRe: Alternate solution Pin
dzCepheus4-May-10 4:58
memberdzCepheus4-May-10 4:58 
GeneralRe: Alternate solution Pin
ricrodrigues4-May-10 5:30
memberricrodrigues4-May-10 5:30 
GeneralRe: Alternate solution Pin
dzCepheus4-May-10 8:21
memberdzCepheus4-May-10 8:21 
GeneralRe: Alternate solution Pin
ricrodrigues4-May-10 8:33
memberricrodrigues4-May-10 8:33 
Generaltoo complex solution ... Pin
gmt523-May-10 2:07
membergmt523-May-10 2:07 
GeneralRe: too complex solution ... Pin
ricrodrigues3-May-10 3:30
memberricrodrigues3-May-10 3:30 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150901.1 | Last Updated 3 May 2010
Article Copyright 2010 by ricrodrigues
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid