我正在尝试返回一个整数数组,无法让它工作......
下面是我在return array
无法将类型'int []'隐式转换为'int'
public int getIndexes(int num)
{
var wb = (Excel.Workbook)Globals.ThisAddIn.Application.ActiveWorkbook;
var wsEvars = wb.Sheets["Evars"];
Excel.Worksheet sheet = null;
if (num == 0) sheet = wsEvars;
if (num != 2)
{
var rng = (Excel.Range)sheet.Range[sheet.Cells[3, 2], sheet.Cells[3, 27]];
var wsValues = rng.Cells.Value;
int scopeIndex = 0;
int methodIndex = 0;
int delimiterIndex = 0;
int formatIndex = 0;
int count = 0;
foreach (var head in wsValues)
{
if (head == "*Scope")
scopeIndex = count + 1;
if (head == "Set Method")
methodIndex = count + 1;
if (head == "Delimiter")
delimiterIndex = count + 1;
if (head == "Format")
formatIndex = count + 1;
}
int[] array = new int[]{scopeIndex, methodIndex, delimiterIndex, formatIndex};
return array;
}
}
答案 0 :(得分:2)
问题是您的方法正在返回int
而不是int[]
。此外,哟正在if
块内进行返回,这将不会确保return
将被命中,因为这只会在条件为真时发生。如果条件是假的怎么办?在这种情况下你不会返回任何东西。从块中获取return
:
public int[] getIndexes(int num)
{
int[] array = null;
var wb = (Excel.Workbook)Globals.ThisAddIn.Application.ActiveWorkbook;
var wsEvars = wb.Sheets["Evars"];
Excel.Worksheet sheet = null;
if (num == 0) sheet = wsEvars;
if (num != 2)
{
var rng = (Excel.Range)sheet.Range[sheet.Cells[3, 2], sheet.Cells[3, 27]];
var wsValues = rng.Cells.Value;
int scopeIndex = 0;
int methodIndex = 0;
int delimiterIndex = 0;
int formatIndex = 0;
int count = 0;
foreach (var head in wsValues)
{
if (head == "*Scope")
scopeIndex = count + 1;
if (head == "Set Method")
methodIndex = count + 1;
if (head == "Delimiter")
delimiterIndex = count + 1;
if (head == "Format")
formatIndex = count + 1;
}
array = new int[]{scopeIndex, methodIndex, delimiterIndex, formatIndex};
}
return array;
}