Empty Array question

Is there a way to know if an array has been populated when its declared like. . .
Dim sArray() As String
All of the functions like Ubound and Lbound raise errors when there is no data in the array.
[215 byte] By [Dino Vaught] at [2007-11-19 23:58:58]
# 1 Re: Empty Array question
There's a few methods that can be found here (http://www.vbforums.com/showthread.php?t=375341).

If you aren't using numbers, you should be able to slide by with the first method (I've tried it with a string array taking strings and it works fine).

If you will use numbers, if the array is a Long, Integer, etc., don't use the CInt, CLng, etc. conversions or the Fix, Round, Int, etc. rounding, but opt for FormatNumber. If the array will be Single or Double, you will have to use FormatNumber for whole numbers (CSng and CDbl will give the error still).

But since the error only occurs in the IDE and you can continue where the error occurs without it reoccuring (unless another line would cause it with a specific or automatic typecast without FormatNumber), it's not fatal.
ChaosTheEternal at 2007-11-9 19:58:29 >
# 2 Re: Empty Array question
Can't you also use If IsEmpty(Array(ArrayIndex)) Then ...
HanneSThEGreaT at 2007-11-9 19:59:33 >
# 3 Re: Empty Array question
Thanks Chaos. Not sure how I missed that one. The sample has everything and more than I needed.

I pulled out the line that's specific to my need. (Not ArrayName) = -1
Works within and outside of the IDE

Private Sub Command1_Click()
Dim sArray() As String

If (Not sArray) = -1 Then
MsgBox "Array not initialized"
Else
MsgBox "Array initialized"
End If

End Sub
Dino Vaught at 2007-11-9 20:00:32 >
# 4 Re: Empty Array question
Can't you also use If IsEmpty(Array(ArrayIndex)) Then ... Unfortunately, IsEmpty doesn't work like you (and I, and probably many others) would expect with arrays.

Just try:Dim sArray() As String
If IsEmpty(sArray()) Then MsgBox "Array is empty"You won't get a message box, as it doesn't consider the array as being empty, and you can't pass an index as the array doesn't have a size (you will get a subscript out of range error).

Even if you explicitly declare a size for the array, but give no values, trying IsEmpty on an index still does not return True.
ChaosTheEternal at 2007-11-9 20:01:32 >
# 5 Re: Empty Array question
Unfortunately, IsEmpty doesn't work like you (and I, and probably many others) would expect with arrays.

Just try:Dim sArray() As String
If IsEmpty(sArray()) Then MsgBox "Array is empty"You won't get a message box, as it doesn't consider the array as being empty, and you can't pass an index as the array doesn't have a size (you will get a subscript out of range error).

Even if you explicitly declare a size for the array, but give no values, trying IsEmpty on an index still does not return True.
Wow! Thanx for clearing that up for me!
HanneSThEGreaT at 2007-11-9 20:02:42 >