Is Array Empty
- From: "MP" <NoSpam@xxxxxxxxxx>
- Date: Thu, 5 Jul 2007 13:11:42 -0500
according to google research there are at least 4 ways to test an array for
'emptiness' <g>
I suppose if you're in California, usa you could also spin a crystal over
it...but that's another story ....<g>
1 IsEmpty function (doesn't work...only tests for initialization, not
emptiness)
2 Ubound = -1 (doesn't work on uninitialized array because ubound
throws error)
3 api call using CopyMemory (in my test blows up ide instantly)
4 trap error on ubound call
here's the api version, posted here in past, that didn't work for me, maybe
it's not supposed to work in ide or some other problem????
also my stab at a general purpose function based on my tests of the above 4
ways...
comments welcome
Thanks
Mark
''note this crashes vb ide instantly in my tests
'janne paakkonen
'You can also put on your black belt and do some API-programming. The
'code below checks "under the hood" if the array's data pointer is
'initialized. Works for all arrays, all dimensions.
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(destination As Any, source As Any, ByVal lBytes As Long)
'Public Function IsArrayEmpty(vAnyArray As Variant) As Boolean
' Dim lAddressArrayDescriptor As Long
' Dim lAddressArrayData As Long
' CopyMemory lAddressArrayDescriptor, ByVal VarPtr(vAnyArray) + 8, 4
' CopyMemory lAddressArrayData, ByVal lAddressArrayDescriptor, 4
' IsArrayEmpty = IIf(lAddressArrayData = 0, True, False)
'End Function
'my attempt
Public Function IsArrayEmpty(vArr As Variant)
'if IsEmpty is going to work, use that
If IsEmpty(vArr) Then
IsArrayEmpty = True
Exit Function
End If
'trap error on uninitialized array
On Error Resume Next
Dim lUb As Long
lUb = UBound(vArr)
If Err Then
Err.Clear
IsArrayEmpty = True
Exit Function
End If
On Error GoTo 0
'if no error, is it negative
If UBound(vArr) = -1 Then
IsArrayEmpty = True
Exit Function
End If
End Function
.
- Follow-Ups:
- Re: Is Array Empty
- From: Tony Proctor
- Re: Is Array Empty
- From: Saga
- Re: Is Array Empty
- From: Jeff Johnson
- Re: Is Array Empty
- Prev by Date: Re: SQL date range problem
- Next by Date: Re: Problem with GetAttr()
- Previous by thread: Modeless form with controls added at runtime
- Next by thread: Re: Is Array Empty
- Index(es):
Relevant Pages
|