C# 使用switch case语句发出
我的代码如下。基本上,我正在读取excel文件并将其内容存储到对象数组中。然后,我使用switch case语句执行不同的操作。请检查下面的我的代码:-C# 使用switch case语句发出,c#,.net,console,console-application,C#,.net,Console,Console Application,我的代码如下。基本上,我正在读取excel文件并将其内容存储到对象数组中。然后,我使用switch case语句执行不同的操作。请检查下面的我的代码:- using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data; using System.Drawing; using System.Com
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;
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用系统数据;
使用系统图;
使用系统组件模型;
使用Excel=Microsoft.Office.Interop.Excel;
使用Microsoft.Office.Interop.Excel;
命名空间Excel1
{
班级计划
{
公共静态void Main(字符串[]args)
//公共服务
{
//字符串str;
Excel.Application xlApp=新的Excel.Application();
Excel.Workbook xlWorkbook=xlApp.Workbooks.Open(@“D:/WebServiceTemplate.xlsx”);
Excel._工作表xlWorksheet=xlWorkbook.Sheets[1];
Excel.Range xlRange=xlWorksheet.UsedRange;
int rowCount=xlRange.Rows.Count;
int colCount=xlRange.Columns.Count;
int numSheets=xlWorkbook.Sheets.Count;
//
//遍历这些工作表。它们从1开始索引。
//
for(int sheetNum=1;sheetNumvalueArray
是一个多维对象数组,根据您的参数定义。switch
不支持这一点
您不能也不会对值数组执行切换;switch
对单个值进行操作。您可以使用foreach
展平数组,迭代每个值,然后应用切换,但是
如错误所示,object
不能在switch
语句中使用,只能键入错误消息中指示的值。如果每个值都是整数,请在开关中将值转换为int
更新好的,现在它们又是字符串了
例如:
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;
}
}
这是非常自我解释的:您不能将switch
语句与valueArray
对象一起使用,因为它的类型(object[,]
)。错误消息指的是开关后括号中变量的类型。在您的情况下,这是一个数组,显然不是(正如错误消息所说的那样)bool、char、string、integral、enum或相应的可空类型。数组不受开关支持。它只接受一个参数,应根据它定义大小写
例:
您好。您必须传递数组元素,但不能传递整个数组。您是否尝试过在至少MSDN中查找开关大小写?大小写中的注释与excel单元格中的值相同…您能否帮助我了解v如何循环通过每个维度并应用d开关?我的仅包含单词之类的字符…所以?只需转换到实际类型是“像单词一样的字符”;因此转换为string
而不是int
,并将大小写更改为“1”
而不是1
(或您感兴趣的任何实际值)。嗨..我按照您的建议做了…但是我得到了这个错误“无法将'System.Double'类型的对象转换为'System.string'switch语句中出现错误。有任何修复吗?好的,因此每个项目的值都是双精度的。改为转换为该值。
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 '/':
...
}