# 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
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
you can check the upperbound and lower bound against its original value
ie: lbound(x) and ubound(x)
0
Commented:
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
Commented:
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
Commented:
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
Author Commented:

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
Commented:
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
Commented:
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
Commented:
Option Explicit

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

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

'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

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author 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
Commented: