[RESOLVED] Count Elements in an array
hi, would like to ask whether there is a command or a function to get the number of elements in an array?
For example i have a data in an array which I will split. So is there a command in VB to check how many elements are there in an array? or to do this will need to loop and add the number of loops?
Please check code below:
Code:
Sub xxx()
Dim i As Integer
Dim split_str() As String
For i = 0 To UBound(split_str)
x = x + 1 ' to get the number of data in an array
Next
End Sub
'is there a command to count the number of elements
without doing the loop?
any ideas is greatly appreciated. thanks.
Re: Count Elements in an array
Directly specifying the command UBound(split_str) produces the number of elements.
Try the below code
Dim a() As String
For i = 1 To 10
ReDim a(i) As String
a(1) = "a"
Next
MsgBox UBound(a)
The result would be 10 since there are ten elements in the array "a"....
Re: Count Elements in an array
You can't loop through an undeclared array. Just like AVIS said, you can access this directly using ubound, but you will have to know the lower bound of your array to get an accurate count.
Code:
Sub xxx()
Dim i As Integer
Dim split_str() As String
For i = 0 To UBound(split_str) <<ERROR
x = x + 1 ' to get the number of data in an array
Next
Code:
Sub xxx()
Dim i As Integer
Dim split_str() As String
split_str = Split("Some String", " ")
'We know split returns a zero based array, so we can use this...
x = Ubound(split_str) + 1
Re: Count Elements in an array