UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> IsArrayInitialized    


Synopsis

With this function, you can determine whether a dynamic array variable already has ReDim statement applied or if it needs to be Redim'd without incurring a runtime error handler. This work by making an API call which can be reliably used to report whether an array is initialized or not. Though the API is intended to return the dimensions, the results returned for an initialized array are not correct and you would just use UBound()/LBound() anyway. However, unlike UBound()/LBound(), it will always return 0 to indicate that array has not been initialized yet.

Sample Usage:

CODE
Private strData() As String 'Module-Level variable
 
Public Function GetCacheData() As String()

 If Not IsArrayInitialized(strData) Then
   Redim strData(GetCountOfElements())
   FillArray strData
 End If
 
 GetCacheData = strData
End Function

The effect is now you only have an array that's built once and on any subsequent calls, re-uses the same array and you don't have to worry about somehow getting uninitialized array by accident.

CODE
' IsArrayInitialized
' http://www.utteraccess.com/wiki/IsArrayInitialized
' Code courtesy of UtterAccess Wiki
' Licensed under Creative Commons License
' http://creativecommons.org/licenses/by-sa/3.0/
'
' You are free to use this code in any application,
' provided this notice is left unchanged.
'
' rev  date                          brief descripton
' 1.0  2011-12-10                    
'
Public Declare Function IsArrayInitialized Lib "OleAut32.dll" Alias "SafeArrayGetDim" ( _
   Var() As Any _
) As Boolean

Creative Commons License
IsArrayInitialized by UtterAccess Wiki is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Editing and revision of the content is freely encouraged; for details, see Expected Usage.

Edit Discussion
Custom Search
Thank you for your support!
This page has been accessed 6,814 times.  This page was last modified 16:16, 11 December 2011 by BananaRepublic.   Disclaimers