Hi All
Is there anywya of knowing whether an array is empty or not. If there is can you tell me how I could do this.
Thanks
Printable View
Hi All
Is there anywya of knowing whether an array is empty or not. If there is can you tell me how I could do this.
Thanks
Code:If UBound(MyArray) = 0 Then MsgBox "EMPTY!"
Dim aMyArray() As String
Dim i As Integer
Dim vIsEmptyTF As Boolean
For i = LBound(aMyArray()) To UBound(aMyArray())
If aMyArray(i) <> "" Then
vIsEmptyTF = False
End If
Next i
We have a different defenition of empty ;)
I guess we do. My Bad vIsEmptyTF = true
I like your way better anyway!
Joe
If you are dealing with numeric expressions, use 0 instead of "".
Code:If aMyArray(i) <> 0 Then
Code:on error resume next
test=aMyarray(i)
if err=9 then msgbox "empty array"
on error goto 0
Hi jo 201 !!!
If you want do know if the array is initialized
(dim myarray(10))
then the best way to check this out is, kedaman approach.
if you wnat to know if the array have values, then
you can use Megatron or courchjo approach, depends which
data type your array is.
-cu TheOnly
You can use the IsEmpty call if you want to see if an array has been initialized or not.
e.g.
[CODE]
If IsEmpty(MyArray) Then
'do something
Else
'do something else
End If
Empty is a value only variants can have.
Not sure about that keda!
Code:Private Sub Command1_Click()
Dim str$
str = ""
MsgBox str = Empty
str = "haha keda's wrong for a change ;P"
MsgBox str = Empty
End Sub
:eek: Aaah! jop got me! :D Actually hehe :p, not really, it convert empty to a nullstring before assigning it to str :)
:eek: I'll never be better than Kedaman :mad:
kedeman's way or this way, similar.
ceri's tested ok as well
Jop, courchjo, Megatron's will creat an error
on execution of code.
Code:'Check to see if an array is empty
'avoid error on trying to view
On Error GoTo QuitNow:
Debug.Print UBound(myArray)
On Error GoTo 0
Exit Sub
QuitNow:
Debug.Print "Error,no dimension for array!"
[Edited by HeSaidJoe on 01-12-2001 at 12:01 PM]
The error nr for "Subscript out of range" is 9, so if your going to implement that in your project, check for Err.Number 9 to make sure you capture the right error.
:DCode:'Jop, not if you use it like this as there is only
'one error that can happen inside the function.
Public Function CheckArray(x As Variant) As Boolean
CheckArray = True
'Check to see if my array is empty
On Error GoTo Quitnow:
Debug.Print UBound(x)
On Error GoTo Quitnow:
Exit Function
Quitnow:
CheckArray = False
End Function
Private Sub Form_Load()
x = CheckArray(myArray)
If x Then
MsgBox "Carry On"
Else
MsgBox "Empty"
Exit Sub
End If
End Sub
Well Wayne, that's what I said :)
Well I should have said:
[qoute]
if your going to implement that in your project (which probably has more error(handlers)), check for Err.Number 9 to make sure you capture the right error.
[/quote]
Jop
I'm just playing with your emotions...slow day at the office.
:D:D:
Hehe what are you doing guys!? this is an old thread, i noticed, who picked it up?
TheOnly