2011-08-25 3 views
-1

인사를 C#을 사용 오류접근 열거 유형 내가 열거 유형의 문제가 윈폼에

이 같은 일을하고 ....

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+ "      
    } 
    } 

} 

나는이 라인 타입 1에 오류가 발생하고있어 = "ALL",톤 * ype2 = "0-500", 타입 3 = "500-1000" * , type4에 = "1000" 암시 적으로 변환 할 수 없습니다라고 입력 문자열이

을 int로 어떻게 데프 내가 할 수있는 열거으로 이러한 이네

이에 하나 PLS 도움말 ......

답변

2

당신은 문자열로 열거 형을 정의 할 수 없습니다 얼마나 내가 ..... 액세스하고 listviewcategoriesitems와 비교할 수

값 - 열거 형은 효과적으로 이름이 지정된 숫자입니다. 당신은 문자열 상수를 원한다면, 당신은 단지 사용해야합니다 :

public const string Type1 = "ALL"; 
public const string Type2 = "0-500"; 

... 등 당신이, 당신은뿐만 아니라 Dictionary<CategoryType, string> 가능성이 역방향 매핑을 만들 수 중 하나를 다른 곳에서 사용하기에 열거 필요한 경우, 또는 실행 시간에 검색 할 수있는 속성 (예 : [Description("ALL")])의 문자열로 각 열거 형 값을 꾸미십시오. 약간 어색하지만 너무 열심히하지는 않습니다.

은 (또한 C 번호는 대소 문자를 구분합니다 -.에서 파생 할 클래스 form가 없습니다, 그리고 다른 개발자를위한 코드를 읽기 쉽게 만들기 위해 .NET naming conventions을 다음 잘 가치)

0

시도
public enum categorytype 
{ 
    ALL=1, 
    From0TO500=2, 
    From500To1000=3 , 
    From1000=4      
} 

그래서 당신은 1,2,3, and 4

if(lstviewcategories.SelectedValue == categorytype.All.ToString()) 
{ 
    .................. 
} 

lstviewcategories 항목의 값을 변경해야합니다 나 ... 그들이 등등 INT, 바이트, 긴, 그리고로만 수를 수용하고, lstviewcategoriesSelectedValue 다음 당신은 EnumsString 값을 할당 할 수 없습니다

categorytype mycategorytype = (categorytype)[Enum].Parse(Typeof(categorytype),lstviewcategories.SelectedValue); 
if(mycategorytype == categorytype.All) 
{ 
    .................. 
} 
0

비교 열거 형을 캐스팅.

0

바와 같이 here on MSDNEnum 됨 같이 String을 단순한 수치 유형에 따라

byte, sbyte, short, ushort, int, uint, long 또는 ulong

그렇게하지 수 언급 당신의 정의.

1

당신은 struct 대신 사용할 수 있습니다

struct CategoryType 
{ 
    public const string Type1 = "ALL"; 
    public const string Type2 = "0-500"; 
    public const string Type3 = "500-1000"; 
    public const string Type4 = "1000+"; 
}