|
-
Oct 23rd, 2009, 12:16 AM
#1
Thread Starter
Addicted Member
[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.
The taller the bamboo grows the lower it bends... 
-
Oct 23rd, 2009, 12:48 AM
#2
New Member
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"....
-
Oct 23rd, 2009, 10:03 AM
#3
Fanatic Member
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
-
Nov 5th, 2009, 09:18 AM
#4
Thread Starter
Addicted Member
Re: Count Elements in an array
thanks
The taller the bamboo grows the lower it bends... 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|