Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

Get the enum value from enum

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);
        }
0
countrymeister
Asked:
countrymeister
  • 5
  • 3
  • 2
  • +2
1 Solution
 
AndyAinscowCommented:
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
0
 
countrymeisterAuthor 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
0
 
AndyAinscowCommented:
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 ?
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
Ken ButtersCommented:
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

0
 
Jacques Bourgeois (James Burger)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
0
 
käµfm³d 👽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

0
 
Jacques Bourgeois (James Burger)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
0
 
käµfm³d 👽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  ; )  ]
0
 
AndyAinscowCommented:
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) ....
0
 
käµfm³d 👽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.
0
 
AndyAinscowCommented:
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
0
 
AndyAinscowCommented:
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"
0
 
countrymeisterAuthor Commented:
I had a solution even before the experts posted their comments, and Ken Butters was another way of doing it.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now