【问题标题】:Enum & Switch Case枚举和切换案例
【发布时间】:2009-05-08 07:34:37
【问题描述】:

您好,我正在使用通过开关转换为字符串的枚举,但它不起作用。它给出了编译错误:无法将类型'userControl_commontop.UserType'隐式转换为'string'

代码是:

private void CommonTopChangesnew(string usertype)
{

    switch (usertype.Trim().ToUpper())
    {
        case UserType.NORMAL :
            hlkSAD.Enabled = false;
            hlkMRTQuery.Enabled = false;
            hlkReqViewer.Enabled = false;
            hlkSendnotif.Enabled = false;
            break;
        case UserType.POWER :
            hlkSAD.Enabled = false;
            hlkReqViewer.Enabled = false;
            hlkSendnotif.Enabled = false;
            break;
    }
}

enum UserType
{
    NORMAL,
    POWER,
    ADMINISTRATOR
}

【问题讨论】:

  • 请重新格式化您的代码片段;目前无法阅读...

标签: asp.net enums switch-statement case


【解决方案1】:

你应该试试这个:

enum UserType
{
  NORMAL,
  POWER,
  ADMINISTRATOR
}

private void CommonTopChangesnew(string usertype)
{
  switch ((UserType)Enum.Parse(typeof(UserType), usertype, true))
  {
    case UserType.NORMAL:
      hlkSAD.Enabled = false;
      hlkMRTQuery.Enabled = false;
      hlkReqViewer.Enabled = false;
      hlkSendnotif.Enabled = false;
      break;
    case UserType.POWER:
      hlkSAD.Enabled = false;
      hlkReqViewer.Enabled = false;
      hlkSendnotif.Enabled = false;
      break;
  }
}

【讨论】:

    【解决方案2】:

    枚举不是字符串,就像常量const int MY_VALUE = 1; 不是字符串一样。

    你应该把你的字符串变成一个枚举:

    switch ((UserType)Enum.Parse(usertype, typeof(UserType))) {
      ...
    }
    

    【讨论】:

    • 第一个选项可能不起作用,因为 switch 要求 case 语句保持不变。
    • @ShaulBehr 我知道这已经过时了……但 Hari 完全正确——第一个例子行不通。那是因为 switch 语句要求 case 保持不变(尽管如果有办法解决这个问题,我会非常渴望听到它)。
    • @Maverick - 承认,尽管几年后!我会更新我的答案。
    【解决方案3】:

    您可以使用此函数将 userType 参数转换为枚举值:

    object Enum.Parse(System.Type enumType, string value, bool ignoreCase);
    

    作为

    UserType utEnum =  Enum.Parse(UserType, userType, true);
    

    然后您可以将您的 switch 语句称为:

    switch (utEnum)
        { ... }
    

    【讨论】:

      【解决方案4】:

      您的函数接受字符串类型的参数,然后您使用相同的参数来比较属于 Enum 的类型。冲突就在这里。

      你的函数应该是:

      private void CommonTopChangesnew(UserType usertype)
      {
      
        switch (usertype)
        {
          case UserType.NORMAL :
            hlkSAD.Enabled = false;
            hlkMRTQuery.Enabled = false;
            hlkReqViewer.Enabled = false;
            hlkSendnotif.Enabled = false;
            break;
          case UserType.POWER :
            hlkSAD.Enabled = false;
            hlkReqViewer.Enabled = false;
            hlkSendnotif.Enabled = false;
            break;
        }
      }
      

      【讨论】:

        【解决方案5】:

        您不能将字符串与枚举进行比较。

        您应该将 Enum 传递给您的方法。

        【讨论】:

          【解决方案6】:

          选项 1: 更改您的 CommonTopChangesnew 以接受 UserType 枚举作为参数

          选项 2: 使用 Enum.Parse 将您的字符串转换为开关块中的 UserType 枚举:

          (UserType)Enum.Parse(typeof(UserType), usertype)

          【讨论】:

            猜你喜欢
            • 2012-09-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-12-14
            • 2017-08-08
            • 1970-01-01
            • 1970-01-01
            • 2021-08-26
            相关资源
            最近更新 更多