2010-07-29 2 views
2

Я занимаюсь разработкой C# приложение, и у меня есть следующее перечисление:Сравнивая значения enumType с Int

public enum TourismItemType : int 
{ 
    Destination = 1, 
    PointOfInterest = 2, 
    Content = 3 
} 

И я также есть ИНТ переменная, и я хочу, чтобы проверить эту переменную, чтобы знать, что равно TourismItemType.Destination, как это:

int tourismType; 
if (int.TryParse(NavigationContext.QueryString.Values.First(), out tourismType)) 
{ 
    switch (tourismType) 
    { 
     case TourismItemType.Destination: 
      ShowDestinationInfo(); 
      break; 
     case TourismItemType.PointOfInterest: 
      ShowPointOfInterestInfo(); 
      break; 
    } 
} 

Но выдает ошибку.

Как я могу это сделать?

Спасибо.

+0

Я знаю, что у вас есть ответы прямо сейчас, но в общем положить в Teh детали ошибки, отбрасывается, а не просто говорю, что там был одним. Даже если ошибка ничего не значит для вас, это может означать что-то другое. – Chris

ответ

5

Ввести tourismType в ваш тип перечисления, поскольку нет никакого неявного преобразования из ints.

switch ((TourismItemType)tourismType) 
//... 
1

Вы можете разобрать tourismType для вашего типа перечислений с помощью Enum.TryParse или вы можете обработать значения перечислений в междунар как: case (int)TourismType.Destination.

1

Попробуйте

int tourismType; 
if (int.TryParse(NavigationContext.QueryString.Values.First(), out tourismType)) 
{ 
    switch (tourismType) 
    { 
     case (int)TourismItemType.Destination: 
      ShowDestinationInfo(); 
      break; 
     case (int)TourismItemType.PointOfInterest: 
      ShowPointOfInterestInfo(); 
      break; 
    } 
} 

или

int tourismType; 
TourismItemType tourismTypeEnum; 
if (int.TryParse(NavigationContext.QueryString.Values.First(), out tourismType)) 
{ 
    tourismTypeEnum = (TourismItemType)tourismType; 
    switch (tourismTypeEnum) 
    { 
     case TourismItemType.Destination: 
      ShowDestinationInfo(); 
      break; 
     case TourismItemType.PointOfInterest: 
      ShowPointOfInterestInfo(); 
      break; 
    } 
} 
1

Вы могли бы тоже делаем:

int tourismType; 
if (int.TryParse(NavigationContext.QueryString.Values.First(), out tourismType) 
{ 
    if (Enum.IsDefined(typeof(TourismItemType), tourismType)) 
    { 
     switch ((TourismItemType)tourismType) 
     { 
      ... 
     } 
    } 
    else 
    { 
     // tourismType not a valid TourismItemType 
    } 
} 
else 
{ 
    // NavigationContext.QueryString.Values.First() not a valid int 
} 

Конечно, вы также можете обращаться с недействительным туристским типом в корпусе коммутатора default:.

2

Если вы используете .NET 4, то вы можете использовать метод Enum.TryParse:

TourismItemType tourismType; 
if (Enum.TryParse(NavigationContext.QueryString.Values.First(), out tourismType)) 
{ 
    switch (tourismType) 
    { 
     case TourismItemType.Destination: 
      ShowDestinationInfo(); 
      break; 
     case TourismItemType.PointOfInterest: 
      ShowPointOfInterestInfo(); 
      break; 
    } 
} 
Смежные вопросы