【问题标题】:Returning multi dimensional array data from C# webservice to vba将多维数组数据从 C# webservice 返回到 vba
【发布时间】:2010-12-07 11:38:40
【问题描述】:

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

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

我有一个 COM Visible C# 类库。这是一个使用此服务并将其原样提供给 Excel VBA 客户端的薄层。 (出于某些原因,我们选择不通过 VSTO 或 Web Services References Toolkit 路由。)

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

我在 VBA 中调用该方法如下。

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

我收到类型不匹配错误。

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

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

【问题讨论】:

    标签: c# arrays vba service


    【解决方案1】:

    锯齿状阵列是可能的。我认为 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
    

    【讨论】:

      猜你喜欢
      • 2012-12-08
      • 1970-01-01
      • 2012-12-19
      • 2014-07-23
      • 1970-01-01
      • 2016-01-25
      • 2019-03-17
      • 2018-01-05
      • 1970-01-01
      相关资源
      最近更新 更多