Get the enum value from enum

countrymeister
countrymeister used Ask the Experts™
on
I have an enum

 enum CommodityCalendar
        {
            January = 'F',
            February = 'G',
            March = 'H',
            April = 'J',
            May = 'K',
            June = 'M',
            July = 'N',
            August = 'Q',
            September = 'U',
            October = 'V',
            November = 'X',
            December = 'Z'
        }

I need to get the values , F, G depending on a string passed to some method

I tried the following, but it retuns the value such as January if I pass 01/10/2014, I need the value F to be returned

 public static void GetLetterCode(string monthName)
        {
           CommodityCalendar calendar = new CommodityCalendar();
           string test = (string)Enum.Parse(typeof(CommodityCalendar), monthName);
        }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AndyAinscowFreelance programmer / Consultant

Commented:
http://msdn.microsoft.com/en-us/library/sbbt4032.aspx

enum should not be used with a string (F, G....) value.  You might be better coding a small class/function to do what you want

Author

Commented:
Andy

Thanks for your comments

Take a look a this

        enum CommodityCalendar
        {
         
            [StringValue("F")]
            January = 'F',
            [StringValue("G")]
            February = 'G',
            [StringValue("H")]
            March = 'H',
            [StringValue("J")]
            April = 'J',
            [StringValue("K")]
            May = 'K',
            [StringValue("M")]
            June = 'M',
            [StringValue("N")]
            July = 'N',
            [StringValue("Q")]
            August = 'Q',
            [StringValue("U")]
            September = 'U',
            [StringValue("V")]
            October = 'V',
            [StringValue("X")]
            November = 'X',
            [StringValue("Z")]
            December = 'Z'
        }

and here is the helpful link

http://www.codeproject.com/Articles/11130/String-Enumerations-in-C which helped me
AndyAinscowFreelance programmer / Consultant

Commented:
Interesting - but I notice that is using a helper class to extract the attribute.  Have you followed the example in the link  rather than attempting to code it yourself ?
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Don't know if this is the best way or not... but it worked:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

enum CommodityCalendar
{
    January = 'F',
    February = 'G',
    March = 'H',
    April = 'J',
    May = 'K',
    June = 'M',
    July = 'N',
    August = 'Q',
    September = 'U',
    October = 'V',
    November = 'X',
    December = 'Z'
}

namespace ConsoleApplication1
{
    public class EnumTest
    {

        public Char GetLetterCode(String monthName)
        {
            Enum myEnum = new CommodityCalendar();

            myEnum = (CommodityCalendar)Enum.Parse(typeof(CommodityCalendar), monthName);

            return (Char) Convert.ToDecimal(Enum.Format(typeof(CommodityCalendar), myEnum, "d"));
        }

        static void Main()
        {
            EnumTest myEnumTest = new EnumTest();
            Char myLetter;

            myLetter = myEnumTest.GetLetterCode("January");

        } 
    }
}

Open in new window

Top Expert 2015

Commented:
When you define the values of an enum as a character, the numeric value of the character is recorded.

Simply cast it to a char to get back the letter:

(char) CommodityCalendar.January
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
I agree with JamesBurger. Another approach you might take, though, is to create a string-typed enum by way of a class instead. For example:

public class CommodityCalendar 
{
    private CommodityCalendar(string value)
    {
        this.Value = value;
    }

    public string Value { get; private set; }
    
    public static readonly January = new CommodityCalendar("F");
    public static readonly February =  new CommodityCalendar("G");
    public static readonly March =  new CommodityCalendar("H");
    public static readonly April =  new CommodityCalendar("J");
    public static readonly May =  new CommodityCalendar("K");
    public static readonly June =  new CommodityCalendar("M");
    public static readonly July =  new CommodityCalendar("N");
    public static readonly August =  new CommodityCalendar("Q");
    public static readonly September =  new CommodityCalendar("U");
    public static readonly October =  new CommodityCalendar("V");
    public static readonly November =  new CommodityCalendar("X");
    public static readonly December =  new CommodityCalendar("Z");
    
    public static CommodityCalendar Parse(string value)
    {
        if (string.Equals(value, CommodityCalendar.January.Value, StringComparison.OrdinalIgnoreCase)) return CommodityCalendar.January;
        if (string.Equals(value, CommodityCalendar.February.Value, StringComparison.OrdinalIgnoreCase)) return CommodityCalendar.February;
        if (string.Equals(value, CommodityCalendar.March.Value, StringComparison.OrdinalIgnoreCase)) return CommodityCalendar.March;
        if (string.Equals(value, CommodityCalendar.April.Value, StringComparison.OrdinalIgnoreCase)) return CommodityCalendar.April;
        if (string.Equals(value, CommodityCalendar.May.Value, StringComparison.OrdinalIgnoreCase)) return CommodityCalendar.May;
        if (string.Equals(value, CommodityCalendar.June.Value, StringComparison.OrdinalIgnoreCase)) return CommodityCalendar.June;
        if (string.Equals(value, CommodityCalendar.July.Value, StringComparison.OrdinalIgnoreCase)) return CommodityCalendar.July;
        if (string.Equals(value, CommodityCalendar.August.Value, StringComparison.OrdinalIgnoreCase)) return CommodityCalendar.August;
        if (string.Equals(value, CommodityCalendar.September.Value, StringComparison.OrdinalIgnoreCase)) return CommodityCalendar.September;
        if (string.Equals(value, CommodityCalendar.November.Value, StringComparison.OrdinalIgnoreCase)) return CommodityCalendar.November;
        if (string.Equals(value, CommodityCalendar.December.Value, StringComparison.OrdinalIgnoreCase)) return CommodityCalendar.December;
        
        throw new ArgumentException("Unrecognized value.", "value");
    }
}

Open in new window


So what you have above is a class that cannot be instaniated externally from the class itself, and you have a number of static instances that cannot be modified--the references themselves are marked readonly and the set is private, so it can only be called from within the class itself. It doesn't use the enum keyword, but it works effectively the same:

CommodityCalendar month = CommodityCalendar.January;

Open in new window


When you want to get the underlying value, you simply grab it from the Value property:

public static void GetLetterCode(string monthName)
{
    CommodityCalendar month = CommodityCalendar.Parse(monthName)
    string test = month.Value;
}

Open in new window

Top Expert 2015

Commented:
What is the advantage of creating a class with 40 lines of code when you can simply do it with a cast?

(char) CommodityCalendar.January
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
In this case, yes, it may be overkill. But if the requirement ever changes, and the code becomes two letters instead of one, then char goes out the window. It was intended simply to show another way. [Anyway, it's only 40 lines of code because I included the Parse method  ; )  ]
AndyAinscowFreelance programmer / Consultant

Commented:
cough, cough.  From the first comment:
You might be better coding a small class/function to do what you want

Not only does that method cope with returning strings but one can also build in support for various things - supply a date, month name as string, month as number (1..12) ....
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Sorry Andy. I completely glossed over that part of your comment. I just saw the link to the enum documentation  : \

The only differentiation I could make would be that my suggested code is by design intended to emulate an enum, whereas "create a class" could be interpreted in a bit more of an open-ended manner.
AndyAinscowFreelance programmer / Consultant

Commented:
Agreed, but it could be used in a single line of code (like an enum can be used simply)
String s1 = Foo.MonthCode("January");
String s2 = Foo.MonthCode(25.12.2000);
rather than with all sorts of casts, parses, converts etc in multiple places
AndyAinscowFreelance programmer / Consultant

Commented:
ps.  Don't forget the original usage (from the question) is the month name is a variable
so it isn't required like:
Foo.January
but rather like:
Foo.s where s is "January"

Author

Commented:
I had a solution even before the experts posted their comments, and Ken Butters was another way of doing it.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial