Сравнение значений enumType с int - PullRequest
2 голосов
/ 29 июля 2010

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

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

И у меня также есть переменная int, и я хочу проверить эту переменную, чтобы узнать, что она равна 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;
    }
}

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

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

Спасибо.

Ответы [ 5 ]

5 голосов
/ 29 июля 2010

Приведите tourismType к вашему типу перечисления, так как нет неявного преобразования из целых чисел.

switch ((TourismItemType)tourismType)
//...
2 голосов
/ 29 июля 2010

Если вы используете .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;
    }
}
1 голос
/ 29 июля 2010

Вы также можете сделать:

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
}

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

1 голос
/ 29 июля 2010

Попробуйте

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 голос
/ 29 июля 2010

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...