读书人

怎么判断数组中是否有内容

发布时间: 2013-12-28 22:19:33 作者: rapoo

如何判断数组中是否有内容
有一个函数,返回值为一个数组,但这个函数有可能返回一个空的数组,函数片段如下:
Public Function ToArray() As String()

Dim result() As String
Dim i As Long

If list_.Count = 0 Then
ToArray = result
Exit Function
End If

ReDim result(list_.Count - 1)

For i = 0 To list_.Count - 1
result(i) = list_.item(i)
Next i

ToArray = result

End Function

当list_.Count为0的时候会返回一个空数组,但在调用此函数的地方得不到list_.Count的值。
这时候应该如何判断返回的数据是不是一个空数组?
避免使用Or Error语句。
IsEmpty IsArray等类似的函数都试过了,也不行。

[解决办法]
Private Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saArray() As Any) As Long 'API判断数组为空或没有初始化
Private Sub Command1_Click()
Dim tmp() As String
If SafeArrayGetDim(tmp) = 0 Then
MsgBox "数组为空或没有初始化"
End If
End Sub

读书人网 >VB

热点推荐