Results 1 to 4 of 4

Thread: [RESOLVED] Count Elements in an array

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    233

    Resolved [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...

  2. #2
    New Member
    Join Date
    May 2009
    Posts
    14

    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"....

  3. #3
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    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

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    233

    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
  •  



Click Here to Expand Forum to Full Width