c# - 将多维数组数据从 C# webservice 返回到 vba

标签 c# arrays vba service

我有一个返回二维数组数据的 C# 网络服务。由于我们不能让 web 服务返回多维数据,我让它返回一个锯齿状的数组。

[OperationContract]
object[][] WSGetData();

我有一个 COM 可见 C# 类库。这是一个薄层,它使用此服务并将其按原样提供给 Excel VBA 客户端。 (出于某些原因,我们选择不通过 VSTO 或 Web 服务引用工具包路线。)

class Wrapper
{
    public object[][] GetData()
    {
        return WSproxy.WSGetData(); //Calling the webservice method
    }
}

我在 VBA 中调用方法如下。

Dim data as Variant
data = wrapperObj.GetData();

我收到类型不匹配错误。

当我更改 Wrapper 类以在返回 VBA 之前将 Web 服务的“锯齿状数组”输出转换为多维输出(即对象 [,])时,它工作正常。但我不想这样做,因为它会影响性能,因为我们将传递大量数据。

请问实现此目标的最佳方法是什么。 感谢任何指示..

最佳答案

锯齿状数组是可能的。我认为 VBA 不喜欢您声明变体的方式。您可能需要将其声明为 Variant 数组。请参阅以下示例:

Sub Test()
    Dim oneToTen(9) As String 'Array 1
    Dim tenTo21(10) As String  'Array 2
    Dim twentyTwoTo23(1) As String  'Array 3
    Dim vArray() As Variant   'Jagged Array (array of arrays)

    'Fill test data in the three arrays
    Dim iCount As Integer
    For iCount = 0 To 9
        oneToTen(iCount) = iCount + 1
    Next iCount
    For iCount = 0 To 10
        tenTo21(iCount) = iCount + 11
    Next iCount
    For iCount = 0 To 1
        twentyTwoTo23(iCount) = iCount + 22
    Next iCount

    'If you uncomment the code below, you will get a type mismatch (probably for the same reason you get it in your webservice)
    'vArray1(0) = oneToTen

    'However, if you REDIM the variant array, you can then set each array into the variant
    Const JAGGED_ARRAY_SIZE = 2 'This will probably require another property on your webservice to see how big your Jagged Array is (e.g.  wrapperObj.GetJaggedArraySize())
    ReDim vArray(JAGGED_ARRAY_SIZE)

    vArray(0) = oneToTen
    vArray(1) = tenTo21
    vArray(2) = twentyTwoTo23

    'Now loop through the jagged array:
    Dim outerLoop As Integer
    Dim innerLoop As Integer
    Dim vCurrentArray As Variant

    'Loop through the arrays in the array and print out the data
    For outerLoop = 0 To JAGGED_ARRAY_SIZE
        For innerLoop = 0 To UBound(vArray(outerLoop))
            Debug.Print "Outer Loop:  " & outerLoop & "  Inner Loop:  " & innerLoop & "  Array Value:  " & vArray(outerLoop)(innerLoop)
        Next innerLoop
    Next outerLoop

End Sub

关于c# - 将多维数组数据从 C# webservice 返回到 vba,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4376169/

相关文章:

javascript - 根据输入将字符串分解为多个对象

vba - 添加水平轴标签 - VBA Excel

vba - 在不运行任何启动 vba 代码的情况下从命令行打开 MS-Access 数据库?

c# - 将所有者授予从另一个线程调用的 MessageBox

持续运行的 C# 代码 - 服务还是单独的线程?

c# - 在列表中查找最近的 sumElement 组合

c# - for 循环中计数器变量的范围是什么?

python - python中两个numpy数组之间的区别

javascript - 将多个对象作为值添加到另一个对象中的键

vba - Word VBA,选择整个文档减去第一页