Link to home
Start Free TrialLog in
Avatar of taplin
taplin

asked on

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.
Avatar of crazyman
crazyman
Flag of United Kingdom of Great Britain and Northern Ireland image

you can check the upperbound and lower bound against its original value
ie: lbound(x) and ubound(x)
Avatar of anthonyc
anthonyc

Easy Way to do this

dim lDummy as long
on error resume next
lDummy = lbound(YourArray)
if err > 0 then msgbox "Array not dimmed!"
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!
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!!
 
Avatar of taplin

ASKER

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

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.

ASKER CERTIFIED SOLUTION
Avatar of anthonyc
anthonyc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of taplin

ASKER

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 (https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=10249417 ) that awards you 50 points also.

Thanks to everyone.
Thanks! Glad I could help!

Cheers!