无法将类型int隐式转换为显式转换出口(是否缺少强制类型转换) [英] cannot implicitly convert type int to an explicit convertion exits (are you missing a cast)

查看:111
本文介绍了无法将类型int隐式转换为显式转换出口(是否缺少强制类型转换)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public enum error
      {
          Info = 0,
          Warg = 1,
          ErrMsg = 2
      }



////情况2,情况显示以下错误
//无法将类型int隐式转换为显式转换出口(您是否缺少类型转换)



//// case 2,case there shows the following error
// cannot implicitly convert type int to an explicit convertion exits (are you missing a cast)

switch (error)
               {
                   case 0:
                       Travel.ErrMsgLblForeColor = Color.Green;
                       break;
                   case 2:
                       Travel.ErrMsgLblForeColor = Color.Yellow;
                       break;
                   case 1:
                       Travel.ErrMsgLblForeColor = Color.Firebrick;
                       break;
               }


请帮助我


please help me

推荐答案

为什么不

Why not

switch(error)
{
  case error.Info: ...
  case error.Warg: ...
  case error.ErrMsg: ...
}


非常糟糕的不支持代码.您为什么认为Color与整数相同?为什么使用立即数而不是枚举?成像您的更改声明-同样,您可以声明由错误严重性值索引的颜色数组,而不是switch.

—SA
Very bad unsupportable code. Why do you think Color is the same as integer? Why do you use immediate constants instead of enumerations? Imaging you change declaration— Also, you could declare array of colors indexed by values of error severity, instead of switch.

—SA


您应使用:
You should use:
case Info:
    // 
    break;
case Warg:
    //
    break;
case ErrMsg:
    //
    break;


如果有正当理由使用int,则:


If there is a valid reason to use an int then:

switch ((int)error)
{
    //
}


...但是很少需要.


... but this is rarely needed.


这篇关于无法将类型int隐式转换为显式转换出口(是否缺少强制类型转换)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆