Determining Array Initialization

Is there a simple method to determine whether a dynamic array has been dimension yet?

For instance, suppose I create an array such as:
    dim x() as Integer

At a later time, I want to determine, before using x in a loop, whether x was ever "Redimmed".

I know I can trap for errors, but it would be nice if the was an IsSomethingSomething function in VB.
LVL 2
taplinAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
anthonycConnect With a Mentor Commented:
Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" _
   Alias "RtlMoveMemory" _
  (pDest As Any, pSrc As Any, ByVal ByteLen As Long)

Public Sub Form_Load()
    Dim a() As Integer
   
    If GetArrayDimensions(VarPtrArray(a)) = 0 Then
        MsgBox "Array not dimmed"
    End If
   
End Sub
Function GetArrayDimensions(ByVal arrPtr As Long) As Integer
   
   Dim address As Long

  'get the address of the SafeArray structure in memory
   CopyMemory address, ByVal arrPtr, ByVal 4
   
  'fill the local variable with the first 2 bytes
  'of the SafeArray structure. These first 2 bytes
  'contain an integer describing the number of dimensions
   CopyMemory GetArrayDimensions, ByVal address, ByVal 2
   
End Function

Function VarPtrArray(arr As Variant) As Long

  'Function to get pointer to the array
   CopyMemory VarPtrArray, ByVal VarPtr(arr) + 8, ByVal 4
   
End Function


this is from www.mvps.org
0
 
crazymanCommented:
you can check the upperbound and lower bound against its original value
ie: lbound(x) and ubound(x)
0
 
anthonycCommented:
Easy Way to do this

dim lDummy as long
on error resume next
lDummy = lbound(YourArray)
if err > 0 then msgbox "Array not dimmed!"
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
mcriderCommented:
Lbound and Ubound will generate an error 9 "Subscript out of range" if you try to use them with a dynamic array that has not been initialized...

You need to do it like this:

    Dim x() As Integer
    Dim iVal As Long
   
    On Error Resume Next
    iVal = -1
    iVal = LBound(x)
    If iVal = -1 Then
        MsgBox "Array not initialized"
    End If


Cheers!
0
 
DrDelphiCommented:
Well, the first time out you are going to be checking for errors, but after that you can check the UBound(Array) and compare that to a variable stored off from iteration to iteration.


Good luck!!
 
0
 
taplinAuthor Commented:
I appreciate everyone's answers & comments.

While your answer is technically correct anthonyc, I mentioned in my question that I knew that I could trap for errors, but was looking for something else.

Any other thoughts?
0
 
mcriderCommented:
There is no VB native function to do this, however you can add the following function:

   Function IsArrayInitialized(Source As Variant) As Boolean
       On Error Resume Next
       iVal = -1
       iVal = LBound(Source)
       If iVal = -1 Then
           IsArrayInitialized = False
       Else
           IsArrayInitialized = True
       End If
   End Function


Then you can do this:

   Dim x() As Integer

   Debug.Print IsArrayInitialized(x)
   ReDim Preserve x(2) As Integer
   Debug.Print IsArrayInitialized(x)

The first time IsArrayInitialized is run, it is false... The second time, it is true...


Cheers!

0
 
anthonycCommented:
mcrider:

your code is the SAME as mine.  You trap on an error as well.  Also, IsArrayInitialized the way you wrote it won't take an array of a type.  It'll only take an array of integers, longs, etc.

0
 
taplinAuthor Commented:
Hmmm.... thank you all very much.

I don't really know where my head is these days... my entire job is writing efficiency code, and for some crazy reason I didn't even think of solving this problem with a simple function routine.  I guess I was just stuck in a mindset of getting VB to do the work for me!

To conclude, I am accepting the answer anthonyc has given.  But, mcrider, if you look around, you'll find a question (http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=10249417 ) that awards you 50 points also.

Thanks to everyone.
0
 
mcriderCommented:
Thanks! Glad I could help!

Cheers!
0
All Courses

From novice to tech pro — start learning today.