-
Enum problem
just a quickie - my head seems to have gone blank
i have an enum
Code:
public class1
{
public enum types
{
car=1,
bike =3,
trailer=5,
lorry = 11
}
}
i have a value ( say 5) - can i get the string ("trailer") into a text box this way ?
Code:
int value = 5;
textbox1.text = ???? <----- get the word 'trailer' to go to text box please
am i doing this the right way round or is an enum not the way i need to do it.
would i be better with a switch/case statement ?
thanks
-
You should use a hashtable if you want to do something like that (dictionary in C#). Otherwise just make an enum of type types (in your case), assign it one of its respective enum values, and then call toString().
-
Code:
enum MyEnum { One, Two, Three }
System.Enum.GetName(typeof(MyEnum), MyEnum.Two);
-
Code:
int value = 5;
textBox1.Text = ((types)value).ToString(); // should be trailer
-
Code:
static partial class Extenstions
{
static public string GetString(this Enum p_enum) {
return Enum.GetName(p_enum.GetType(), p_enum);
}
}
Code:
Numbers k = Numbers.One;
Console.WriteLine(k.GetString()); //One
-
Sorry i thought i had posted back to this thread, must have forgotten the submit button.
both the Theoobe and Magos' solutions were tried and did what i needed - i opted for Magos solution for no particular reason.
Sorry for leaving the thread in flux.