Re: Is Array Empty
- From: "Tony Proctor" <tony_proctor@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 16 Jul 2007 16:22:56 +0100
IsEmpty applies only Variants. It has nothing to do with arrays
Ubound=-1 will only for arrays with a SafeArray descriptor initialised
appropriately (e.g. the Split() and Array() functions do this). It's of no
use for arrays created by yourself
Tony Proctor
"MP" <NoSpam@xxxxxxxxxx> wrote in message
news:OfCEw$yvHHA.3356@xxxxxxxxxxxxxxxxxxxxxxx
according to google research there are at least 4 ways to test an arrayfor
'emptiness' <g>maybe
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,
it's not supposed to work in ide or some other problem????4
also my stab at a general purpose function based on my tests of the above
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: MP
- Re: Is Array Empty
- References:
- Is Array Empty
- From: MP
- Is Array Empty
- Prev by Date: Re: Changing Launch Icon Default Properties
- Next by Date: Re: Dir Problem
- Previous by thread: Re: Is Array Empty
- Next by thread: Re: Is Array Empty
- Index(es):
Relevant Pages
|