Issue using switch case statement [closed]

南楼画角 提交于 2019-12-02 13:45:48

valueArray is a multidimensional array of objects, according to your parameter definition. switch does not support that.

  1. You can't and wouldn't perform a switch on an array of values; switch operates on a single value. You can use foreach to flatten the array, iterate over each value, and apply the switch, however...
  2. As the error indicates, object cannot be used in switch statements, only types those indicated in the error message. If each value is an integer, cast the value to an int in the switch.

Update OK, now they are strings again.

Example:

foreach(var value in valueArray)
{
    switch(value as string)
    {
        case "ITemplate.GetAllTemplate":
                    break;
        case "ITask.GetTaskInstanceFromTemplate":
                    break;
        case "CreateTask":
                    break;
        case "UpdateDatabase":
                    break;
        case "GetTaskStatus":
                    break;
        case "VerifyValue":
                    break;
    }
}

The error message refers to the type of the variable in the brackets after your switch. In your case, this is an array, so obviously not (as the error message says) a bool, char, string, integral, enum, or corresponding nullable type.

It is quite self explaining: you can't use the switch statement with valueArray object because of its type (object[,]).

Arrays are not supported by Switch. It takes a single argument and Cases should be defined according to it.

Ex:

     int num=3;
     switch(num)  //in case of integer type
         Case 1:
         Case 2:
         ...
     }


     char ch='a';
     switch(ch)  //in case of character type
     {
         Case 'a':
         Case 'b':
         Case '/':
         ...
     }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!