使用开关case语句问题 [英] Issue using switch case statement

查看:194
本文介绍了使用开关case语句问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下。基本上,我读一个Excel文件及其内容存储到一个对象数组。然后,我使用一个开关case语句做不同的操作。检查下面我的代码: -

My code is as follows. Basically, I am reading an excel file and storing its contents into an object array. Then, I use a switch case statement to do different operations. Check my code below:-

  using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Drawing;
using System.ComponentModel;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
namespace Excel1
{
    class Program
    {
        public static void Main(string[] args)
        //public void ExcelOps()
        {
            //string str;
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"D:/WebServiceTemplate.xlsx");
            Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            Excel.Range xlRange = xlWorksheet.UsedRange;
            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;
            int numSheets = xlWorkbook.Sheets.Count;
            //
            // Iterate through the sheets. They are indexed starting at 1.
            //
            for (int sheetNum = 1; sheetNum <=1; sheetNum++)
            {
                Worksheet sheet = (Worksheet)xlWorkbook.Sheets[sheetNum];
                //
                // Take the used range of the sheet. Finally, get an object array of all
                // of the cells in the sheet (their values). 
                //
                object[,] valueArray = (object[,])xlRange.get_Value(XlRangeValueDataType.xlRangeValueDefault);

                //
                // Do something with the data in the array with a custom method.
                //                
                ProcessInput(valueArray);
            }
        }
        public static void ProcessInput(object[,] valueArray)
        {
            foreach (var value in valueArray)
            {
                switch ((string)value.ToString())
                {
                    case "ITemplate.GetAllTemplate":
                        {
                            //ITemplate.GetAllTemplate
                            break;
                        }
                    case "ITask.GetTaskInstanceFromTemplate":
                        {
                            //ITask.GetTaskInstanceFromTemplate
                            break;
                        }
                    case "CreateTask":
                        {
                            //CreateTask
                            break;
                        }
                    case "UpdateDatabase":
                        {
                            //UpdateDatabase
                            break;
                        }
                    case "GetTaskStatus":
                        {
                            //GetTaskStatus
                            break;
                        }
                    case "VerifyValue":
                        {
                            //VerifyValue
                        }
                        break;
                }
            }
        }
    }
}

当我建立它,我得到一个错误
对象引用未设置到对象的实例。结果
错误出现在switch语句

When I build it, I get an error Object reference not set to an instance of an object.
the error appears in the switch statement

有人可以帮助我?

推荐答案

valueArray 为对象的多维数组,根据你的参数定义。 开关不支持。

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


  1. 您不能,也不会值的阵列上执行开关; 开关运行在一个单一的价值。您可以使用的foreach 扁平化数组,迭代每个值,并应用交换机,但是...

  2. 当误差表示,对象不能在开关语句中使用,只有那些类型错误消息中指出。如果每个值是一个整数,施放价值为int在开关

  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.

例如:

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;
    }
}

这篇关于使用开关case语句问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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