【问题标题】:Is Nothing type mismatch VB6没有什么类型不匹配 VB6
【发布时间】:2016-02-04 18:56:32
【问题描述】:

我尝试了一切,但一切都让我类型不匹配:

Type UserType
...
End Type

Dim SomeArray() As UserType
...
If SomeArray() Is Nothing Then <do smth>
If SomeArray() Is Empty Then <do smth>
If SomeArray Is Nothing Then <do smth>
If SomeArray Is Empty Then <do smth>

我确实想知道我的用户定义类型数组中什么时候没有元素!因为如果我可以使用 VB6 的可能性,我不想使用额外的变量。

我会用

Erase SomeArray

当它的大小 = 1 (UBound(SomeArray) = 1) 并且我想删除最后一个元素时。

我做错了什么?呵呵

【问题讨论】:

    标签: vb6 basic nothing


    【解决方案1】:

    VB6 "Is Nothing" 适用于对象,而不适用于 VB6 arrays

    “Ubound(myarray)”或“Ubound - LBound”是VB6中确定数组当前长度的方法。

    仅供参考,使用 VB6 Collection 可能更适合您。

    【讨论】:

    • 集合不适用于用户类型。它写了类似“你被强迫混蛋吸我的制造!在公共预编译模块中链接你自己,后期绑定的渣滓!”之类的东西。我唯一明白的——“渣滓”。这是非常无礼的。更有什者!我什至不能将简单的字符串添加到集合中! O_O
    【解决方案2】:

    嘿,我从 VBForums "VB6 - Returning/Detecting Empty Arrays" 中找到了解决这个问题的方法。 B)

    (L/UBound 不适用于空数组 - 它返回超出范围的下标。;))

    所以...

    Private Declare Function ArrPtr Lib "msvbvm60" Alias "VarPtr" (Ptr() As Any) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
    
    Public Function Peek(ByVal lPtr As Long) As Long
        Call CopyMemory(Peek, ByVal lPtr, 4)
    End Function
    

    记住在定义这个子程序之前声明你自己的变量!否则会导致一些奇怪的错误(VB突然把我的Exit Sub语句替换为Exit Function!)。

    然后我用了

        If Peek(ArrPtr(SomeArray)) = 0 Then
            MsgBox "Looks like empty array SomeArray() before ReDim ^_^"
        End If
    

        Erase SomeArray
    
        If Peek(ArrPtr(SomeArray)) = 0 Then
            MsgBox "Looks like empty array SomeArray() after Erase ^_^"
        End If
    

    一切正常!

    不是很简单,但很好。

    谢谢大家,我将学习这个名为 Collection 的链表。

    特别感谢 VBForums,他们真的是极客。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多