|
|
SynopsisWith 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/index.php/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
|
| This page was last modified 16:16, 11 December 2011. This page has been accessed 227 times. Disclaimers |