• VB 如何判断数组为空


    方法一:Error方法

    就是采用通用的错误捕获功能

    On Error Goto 行号       '遇到错误,转到行号处处理

    On Error Resume Next '忽略错误,继续执行

    On Error Goto 0           '强制取消错误捕获功能

    Function IsNotEmpty(ByVal sArray As Variant) As Boolean '判断数组是否为空

            Dim i     As Long

            IsNotEmpty = True

            On Error GoTo lerr:

            i = UBound(b)

            Exit Function

    lerr:

            IsNotEmpty = False

    End Function

    方法二:CopyMemory方法

    VB的数组都是安全数组,通过访问一个结构来确定 数组内容保存位置,上标下标和维数

    安全数组结构的地址可以用

    Private Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (ByRef Ptr() As Any) As Long

    Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" _

                 (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)

    安全数组的头两位就保存着维数信息

    Option Explicit

    Private Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (ByRef Ptr() As Any) As Long

    Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)

    Private Sub Form_Load()

        Dim MyArr() As Long

        Dim pMyarr As Long

        Dim nDims As Integer

        '从数据指针得到SafeArray结构的指针

        CopyMemory pMyarr, ByVal VarPtrArray(MyArr), 4

        If pMyarr = 0 Then

            MsgBox "这个数组是空数组"

            '再从这个指针所指地址的头两个字节取出cDims

            CopyMemory nDims, ByVal pMyarr, 2

            MsgBox "这个数组有" & nDims & "维"

        End If

    End Sub

    方法三:使用api函数safearraygetdim()的返回值,返回值值<=0,说明数组元素个数为0或者数组还没有初始化.

    SafeArrayGetDim用来判断一个数组的维数,该函数在MSDN中定义为:

    UINT SafeArrayGetDim(

      SAFEARRAY FAR* psa 

    转换维VB中的语法格式为:

    Public Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saArray() As Any) As Long

    如果数组已经初始化,则返回非0,否则返回0。

    'API判断数组为空或没有初始化

    Sub diag()

    Dim msg As String

    Dim arr1() As String, arr2() As String, arr3() As Date, arr4() As Date, arr5() As Range, arr6() As Range

    msg = "arr1 " & IIf(SafeArrayGetDim(arr1) > 0, "数组不为空!", "数组为空!")

    arr2 = Split("一、二、三、四、五、六", "、")

    msg = msg & vbCrLf & "arr2 " & IIf(SafeArrayGetDim(arr2) > 0, "数组不为空!", "数组为空!")

    msg = msg & vbCrLf & "arr3 " & IIf(SafeArrayGetDim(arr3) > 0, "数组不为空!", "数组为空!")

    ReDim arr4(1 To 100)

    msg = msg & vbCrLf & "arr4 " & IIf(SafeArrayGetDim(arr4) > 0, "数组不为空!", "数组为空!")

    ReDim arr6(1 To 256, 1 To 65536)

    msg = msg & vbCrLf & "arr5 " & IIf(SafeArrayGetDim(arr5) > 0, "数组不为空!", "数组为空!")

    msg = msg & vbCrLf & "arr6 " & IIf(SafeArrayGetDim(arr6) > 0, "数组不为空!", "数组为空!")

    MsgBox msg

    End Sub

    方法四:使用cstr(Join(list[, delimiter]))函数的返回值是否不等于""

    将delimiter参数设置为""

    例如:if (cstr(join(arr,""))) = "" then msgbox "arr 数组为空或者尚未初始化"

  • 相关阅读:
    抽象类与抽象方法
    简单工厂模式
    面向对象的七种基本设计原则
    HashTable集合遍历的三种方法
    继承(父类为虚方法以及子类的重写)
    继承(is与as)
    Chrome OS 更新新版本可让Linux访问USB连接的Android设备
    谷歌对Intel 10nm进度不满
    盖茨对没能做好手机系统对抗苹果表示遗憾
    微软内部封杀 Slack
  • 原文地址:https://www.cnblogs.com/rosesmall/p/14425647.html
Copyright © 2020-2023  润新知