簡體   English   中英

如何使用C#Interop返回整數數組?

[英]How to return an array of integers using C# Interop?

我正在嘗試返回整數數組,但無法使其正常工作...

在ID下面的我的代碼在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;
        }
    }

問題在於您的方法將返回一個int而不是您想要的int[] 另外,喲正在if塊內執行返回操作,這將不能確保return會被擊中,因為只有在條件為true的情況下才會發生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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM