Re: Smart way to connect enums to string values

From: Štefan Šimek (simek.blah_at_kascomp.blah.sk)
Date: 10/12/04


Date: Tue, 12 Oct 2004 15:45:45 +0200


"Henke" <henke_nord@hotmail.com> wrote in message
news:e4i7HBGsEHA.3980@TK2MSFTNGP12.phx.gbl...
> Hi!
> Has anyone a nice way to connect a enum to a string value.
> Now I declare a const string [] in the same class as my enum is defined.
> But I doubt that's the "object oriented" way of doing it.
> Any better suggestions?
>
> Thanks in advance?
> /Henke
>

I don't know if I understand your question well, but you might like to have
a look at the
Enum.ToString() and Enum.Parse() functions, like:

enum Test { One, Two, Three };

public static void Main(string[] args)
{
    Test tst = Test.One;
    string str = tst.ToString();
    Test tst2 = (Test) Enum.Parse(typeof(Test), str);

    Console.WriteLine("tst = {0}\nstr = {1}\ntst2 = {2}", tst, str, tst2);
}

HTH,
Stefan



Relevant Pages

  • Re: enums: What the H? again
    ... > find no sign of it in the Enum class until I did a decompile and saw ... > private String shortName; ... > private static InDir currentState = UNKNOWN; ...
    (comp.lang.java.programmer)
  • Re: enums: What the H? again
    ... Everything comes clear when you decompile an enum, ... private String shortName; ... private static InDir currentState = UNKNOWN; ...
    (comp.lang.java.programmer)
  • A Class to get the name of Enum constant at runtime
    ... Dim rs As ADODB.Recordset ... You can get the description of an Enum form it's value. ... EnumName As String, ... Dim Idx As Long ...
    (microsoft.public.vb.general.discussion)
  • EnumConverter interfering with Designer-Generated Code
    ... string representations of an enum's values. ... every enum whose values I desire to be displayed as localized ... "ShortDistance" but the Designer-Generated code looks like this: ... Design-Time or at Run-Time (so that I can use the localization ...
    (microsoft.public.dotnet.languages.csharp)
  • EnumConverter interfering with Designer-Generated Code
    ... string representations of an enum's values. ... every enum whose values I desire to be displayed as localized ... "ShortDistance" but the Designer-Generated code looks like this: ... Design-Time or at Run-Time (so that I can use the localization ...
    (microsoft.public.vsnet.ide)