如何使用 C# Interop return 整数数组?
How to return an array of integers using C# Interop?
我正在尝试 return 一个整数数组,但无法让它工作...
下面是我的代码,在 return array
上有以下错误
Cannot implicitly convert type 'int[]' to '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;
}
}
问题是您的方法是 returning int
而不是您想要的 int[]
。此外,你在 if
块内执行 return,这并不能确保 return
会被命中,因为只有条件为真时才会发生。如果条件为假怎么办?在这种情况下,您没有 returning 任何东西。将 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;
}
我正在尝试 return 一个整数数组,但无法让它工作...
下面是我的代码,在 return array
Cannot implicitly convert type 'int[]' to '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;
}
}
问题是您的方法是 returning int
而不是您想要的 int[]
。此外,你在 if
块内执行 return,这并不能确保 return
会被命中,因为只有条件为真时才会发生。如果条件为假怎么办?在这种情况下,您没有 returning 任何东西。将 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;
}