hi i have problem with enum types
I am doing like this….
namspace XXXXXXxx
{
public partial class form1:form
{
////////
and i am checking the listview selected item with enum type by the following code
private void lstviewcategories_SelectedIndexChanged(object sender, EventArgs e)
{
if (lstviewcategories.SelectedItems[0].ToString() == categorytype.type1.ToString())
{
/////
blah blah...
}
}
and at here i am defining enum like this...
public enum categorytype
{
type1 = "ALL",
type2 ="0-500",
type3 ="500-1000" ,
type4 ="1000+ "
}
}
}
I am getting error at these lines type1 = “ALL”, t*ype2 =”0-500″,type3 =”500-1000″* ,type4 =”1000+” say cannot implicitly convert type string to int
how can i defined these as enum
how can i access and compare with listviewcategoriesitems…..
would any one pls help on this……
You can’t define enums as string values – enums are effectively named numbers. If you want string constants, you’ll just have to use:
… etc. If you need an enum for use elsewhere, you could either create a
Dictionary<CategoryType, string>and possibly a reverse mapping as well, or decorate each enum value with a string in an attribute (e.g.[Description("ALL")]) which you can retrieve at execution time. It’s slightly awkward, but not too hard.(Also note that C# is case-sensitive – there’s no class
formto derive from, and it’s well worth following the .NET naming conventions to make your code easier to read for other developers.)