将数组从 vba 传递到 c# 时,数据类型不匹配在哪里?

Where is the data type mismatch when passing arrays from vba to c#?

我的 C# COM DLL 有一个接受浮点数组和长整型数组的方法。它 returns 一个浮点数。

从 VBA 在 MS Access 模块中,我创建了一个类型为 single 的数组和另一个类型为 long 的数组,填充它们,创建 DLL app.class 对象,然后用这两个调用它的方法阵列。但是我收到 "type mismatch" 错误。

以下是实际代码,但它很简单,因为我试图在添加 "real" 代码之前解决通信问题。

C#代码:

public float JustTesting(float[] Array1, long[] Array2)
{
    return 96.0F;
}

VBA代码:

Public Sub Test()
    Dim a1(0 To 0) As Single, a2(0 To 0) As Long, sng As Single
    a1(0) = 5
    a2(0) = 10
    Dim o As Variant
    Set o = CreateObject("MyApp.MyClass")
    sng = o.JustTesting(a1, a2)
    Debug.Print CStr(sng)
    Set o = Nothing
End Sub

哪里的数据类型不匹配?

VBA 中的 Long 只有 32 位,与 C# 中的 int 相同。所以你的方法需要采用 ints

的数组
public float JustTesting(float[] Array1, int[] Array2)
{
    return 96.0F;
}