Hi,
Today, I will show you how you can convert the Enum type to a string in .Net. In my demo, I use C#.
Say for example, you have a dropdown where values and texts are list of genders (Male, Female, Gay, Tomboy, Idk),...
and you want to get its preferred partner based on the selected drop down.
public enum Gender
{
MALE,
FEMALE,
GAY,
TOMBOY,
IDK
}
public class Dummy
{
public string GetPreferredPartner(Gender g)
{
if(g == g.MALE)
return "pretty woman";
else if(g == g.FEMALE)
return "handsome guy";
else if(g == g.GAY)
return "macho man";
else if(g == g.TOMBOY)
return "hot mama";
else if(g == g.IDK)
return "macho man";
}
}
//get the selected gender from the drop down
string strGender = this.ddlGender.SelectedValue;
///Here is the conversion of string to an Enumeration
Gender gender = (Gender)Enum.Parse(typeof(Gender), strGender, true);
//the first parameter is the Enum type you want your string to convert to
//the second is the string which will be converted to the specified Enum type
//the third parameter is to determine if the string is case sensitive
That's all, thank you.
-Jygz
jjygot@gmail.com
No comments:
Post a Comment