我正在开发一个C#应用程序,我有以下枚举:
public enum TourismItemType : int
{
Destination = 1,
PointOfInterest = 2,
Content = 3
}
Run Code Online (Sandbox Code Playgroud)
我也有一个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;
}
}
Run Code Online (Sandbox Code Playgroud)
但它会引发错误.
我怎样才能做到这一点?
谢谢.
铸造tourismType
你的枚举类型,因为是整数的隐式转换.
switch ((TourismItemType)tourismType)
//...
Run Code Online (Sandbox Code Playgroud)