变体数组可以有 0 个元素吗?

发布于 2024-12-13 13:45:40 字数 225 浏览 3 评论 0原文

普通动态数组支持空(= nil, Length() = 0)

然而,变体数组似乎不支持这一点。

我在变体数组中传递数据(因为 OLE/COM),当用户定义 0 个元素时,我收到错误...

我可以使用 varEmpty 而不是 0 长度数组,但这些数据数组可以灵活更改(添加项目) 、删除项目等)。

如何在变体中传递空数组,还是需要使用其他方式?

The normal dynamic array supports empty (= nil, Length() = 0).

The variant array however does not seem to support this.

I pass my data in variant array (because of OLE/COM), and I get an error when the user defines 0 elements...

I can use varEmpty instead of a 0 length array, but these data arrays are flexible changed (add item, remove item, etc.).

How I can I pass empty arrays in a Variant, or do I need to use other way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_失温 2024-12-20 13:45:40

varEmpty 是处理此问题的正确方法。当然,COM 接口另一端的代码可能不喜欢空数组,但这一切都取决于您与该接口的特定约定。

varEmpty is the correct way to handle this. Of course, the code on the other side of the COM interface may not like empty arrays, but that all depends on the particular contract you have with that interface.

中二柚 2024-12-20 13:45:40

下面是一些代码,无论数组是否为空,都会更新变体数组。

Sub Variant_Add(Var_Array As Variant, What)
    ' Add "What" to a "Var_Array" without book keeping.
    ' 3/18/19 Created, Mac Lingo

    ' Note: Variant_Add must be defined in the following way: _
        Dim Var_Array as Variant

    Prog = "Variant_Add"

    If IsEmpty(Var_Array) Then
        ReDim Var_Array(1) As Variant
        Knt = 1
    Else
        Knt = Var_Array(0) + 1
        ReDim Preserve Var_Array(Knt) As Variant
    End If

    Var_Array(Knt) = What
    Var_Array(0) = Knt

End Sub ' Variant_Add

Here is some code that will update a Variant Array whether the Array is empty or not.

Sub Variant_Add(Var_Array As Variant, What)
    ' Add "What" to a "Var_Array" without book keeping.
    ' 3/18/19 Created, Mac Lingo

    ' Note: Variant_Add must be defined in the following way: _
        Dim Var_Array as Variant

    Prog = "Variant_Add"

    If IsEmpty(Var_Array) Then
        ReDim Var_Array(1) As Variant
        Knt = 1
    Else
        Knt = Var_Array(0) + 1
        ReDim Preserve Var_Array(Knt) As Variant
    End If

    Var_Array(Knt) = What
    Var_Array(0) = Knt

End Sub ' Variant_Add
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文