enum Q

  • Thread starter Thread starter JezB
  • Start date Start date
J

JezB

Given an enum :

public enum FlavourCategory {None = -1, Personal = -3, Local = -2,
Predefined = 1};

How can I convert a string eg. "-2" into a FlavourCategory ? I dont want to
write a switch statement.
 
JezB said:
Given an enum :

public enum FlavourCategory {None = -1, Personal = -3, Local = -2,
Predefined = 1};

How can I convert a string eg. "-2" into a FlavourCategory ? I dont want to
write a switch statement.

Just convert it to an int and then cast it:

int foo = int.Parse("-2");
FlavourCategory bar = (FlavourCategory) foo;
 
Hi,

string categoryName = ((FlavourCategory)(-2)).ToString();

categoryName, in this case, will have the value "Local".

HTH,
APG
 
Back
Top