Array Bounds

Is there anyway to determine if an array has been dimensioned?

UBound(arrTest()) will give <Subscript out of range> error if the array is not dimensioned.

I can always set a flag if the array is initialized but gets complicated in my application. It is do-able but just curious if there is an easier way...

Thanks,
KAL

[334 byte] By [KAL9] at [2008-1-25]
# 1
Assuming I understand your question... If an array hasn't been dimensioned, its value should be Nothing. So you can test:

If Not arrTest Is Nothing Then
upperBound = UBound(arrTest)
End If

If that didn't answer your question, could you post a code snippet demonstrating the problem, including how arrTest is declared and/or initialized?
JamesKovacs at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2
Even using your example I get an error:

Dim arrTest() As Integer

If Not arrTest Is Nothing Then
MsgBox "Upper bound is " & UBound(arrTest())
Else
MsgBox "arrTest() is undimensioned"
End If
I get "type mismatch" on the line: If Not arrTest is Nothing Then

KAL

KAL9 at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
I ran your code in both VS.NET 2003 and VS 2005 without issue. The expected message box displaying "arrTest() is undimensioned" is displayed. One syntax issue: UBound(arrTest()) should be UBound(arrTest).

Which version of VS are you running that is giving the error? Do you have Option Explicit On and Option Strict On?

JamesKovacs at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4
I'm running VB 6 (SP6). I am using Option Explicit but not Option Strict.

KAL

KAL9 at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5
That would explain it. I was assuming that you were using VB.NET as this newsgroup is intended for VS 2005 and VB.NET 2.0. As far as I know, you can't determine if an array is uninitialized in VB6. You can check out this post (http://www.vbwm.com/forums/topic.asp?TOPIC_ID=3969) for a workaround, though I cringe at the use of On Error Resume Next. If you use that technique, I would highly recommend setting On Error Goto 0 to turn normal error handling back on afterward.
JamesKovacs at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6
I figured as much...thanks for your help though.

I wouldn't use the On Error method either...but I was unsure how to turn on normal error handling (Goto 0)...now I know Smile

KAL

KAL9 at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7
you don't need to use the on Error approach,

See:
http://groups-beta.google.com/group/microsoft.public.vb.general.discussion/msg/ff2280e20095b29c

for an example

BillMcC at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 8

Ooooooh Bill...that makes On Error Resume Next seem elegant!!! ;o>

Weevie at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic General...