Re: Is Array Empty

Tech-Archive recommends: Fix windows errors by optimizing your registry



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 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







.



Relevant Pages

  • Re: Passing a function as a parameter
    ... Dim FunctionArg1 as variant ... AddressOf operator will be useless. ... I have a qsort procedure for arrays of simple variable types (e.g., Longs, ...
    (microsoft.public.excel.programming)
  • Re: sharing a collection between vbsript and vb6
    ... > call it from VB6? ... VBS is limited to variant data types. ... subtype. ... are differently structured than are collections, and even VBS's arrays are ...
    (microsoft.public.scripting.vbscript)
  • Re: WIA Automation Layer problems
    ... or arrays into that method - it passes an object. ... > is of type VARIANT in the wrapper. ... > SAFEARRAY* psa; ...
    (microsoft.public.win32.programmer.mmedia)
  • Re: WIA Automation Layer problems
    ... or arrays into that method - it passes an object. ... > is of type VARIANT in the wrapper. ... > SAFEARRAY* psa; ...
    (microsoft.public.win32.programmer.ole)
  • Re: Matrix Operations in VBA
    ... and I am finding a variety of implimintations using arrays of doubles, ... and variants - some with Option Base 1 and ... It seems like when a single column Range is assigned to a variant, ... The transpose didn't appear to work: the column vector remains a column ...
    (microsoft.public.excel.programming)