Revision: 5858
Updated Code
at April 8, 2008 17:01 by paragjagdale
Updated Code
public enum PayTypeList
{
Unknown,
Hourly,
Salary,
}
PayTypeList payType = (PayTypeList)Enum.Parse(typeof(PayTypeList), "Hourly");
// Throws an ArgumentException if the string is found not to be one of the members of the enum (in this case, if you pass in a string "Weekly" instead of "Hourly", it will throw an ArgumentException)
Revision: 5857
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at April 8, 2008 11:56 by paragjagdale
Initial Code
public PayTypeList StringToYourEnumType(string Paytype)
{
foreach (FieldInfo fi in typeof(PayTypeList).GetFields(BindingFlags.Public | BindingFlags.Static))
{
if (Paytype.CompareTo(fi.Name.ToString().ToLower()) == 0)
{
return (PayTypeList)fi.GetValue(null);
}
}
return PayTypeList.Unknown;
}
// Useage
public enum PayTypeList
{
Unknown,
Hourly,
Salary,
}
PayTypeList payType = ConvertStateEnumeration("Hourly");
Initial URL
Initial Description
This snippet accepts a string which is assumed to be the string version of one of the members and returns an enum object with the respective member selected.* *Thanks to user: mcbutterbuns for improving upon my version of this snippet
Initial Title
Convert a string to its respective Enum member
Initial Tags
Initial Language
C#