Results 1 to 13 of 13

Thread: [RESOLVED] how to find the length of array in v.b 6.0

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    Resolved [RESOLVED] how to find the length of array in v.b 6.0

    hi everybody,

    in my application, i have to find the length of an array, is there any method to find the lenght of an arrray?(it should say how many elements are presented in that array).

    i tried with "UBOUND" method but it gives the original sizeof the the array, but not the how many elements that r presented in that array....
    If u know kindly help me. i stucked in my project for this only..

    Thanks in advance,
    Regards:
    raghunadhs

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: how to find the length of array in v.b 6.0

    UBound is what you need:
    Code:
    Private Sub Command1_Click()
    Dim arItems() As String
    Dim arItems2(3, 5) As Integer
    
        ReDim arItems(3) '4items
        
        'for one dimentional array simply use Ubound
        Debug.Print "arItems contains " & UBound(arItems) + 1
        
        'for multidimentional array specify dimention
        Debug.Print "arItems2 contains - 1st dimention: " & UBound(arItems2, 1) + 1
        Debug.Print "arItems2 contains - 2nd dimention: " & UBound(arItems2, 2) + 1
    
    End Sub

  3. #3
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: how to find the length of array in v.b 6.0

    Rhino Bull

    Code:
    Private Sub Command1_Click()
    Dim test(10) As String
    
    test(0) = "john"
    test(1) = "michel"
    MsgBox UBound(test)
    End Sub
    Now the messge says 10 while only two elements are filled. how i do count only the filled eliments? this is what ragunath expecting i think.
    Last edited by Fazi; Jun 25th, 2007 at 07:53 AM.

  4. #4

  5. #5
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: how to find the length of array in v.b 6.0

    ragunatan,

    i think you will have to loop through your array uing lboud() to uboud() and count how many elemets value are <> "". Record each one which is <> ""

  6. #6
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: how to find the length of array in v.b 6.0

    Oops, Rihno is back.

  7. #7
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: how to find the length of array in v.b 6.0

    Or, depending on the way you fill your array, you could ReDim it everytime you add an item.

    You could add 1 to the UBound of the array each time before you add another element.
    Then, using UBound you do get the correct number.

  8. #8
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: how to find the length of array in v.b 6.0

    Validating agains empty string (or zero for numeric types) might not give correct results - either value could be valid so you'd have initialize array by populating each item with some default value.
    This could very costly (memory and performance)...

    BETTER approach is to use DYNAMIC ARRAY

    where items are added when you need to add one.

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: how to find the length of array in v.b 6.0

    Yes, or a collection/dictionary which manages the resizing internally, which is a bit more efficient than manually resizing it every time you need to add an element.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    Re: how to find the length of array in v.b 6.0

    Thanks all,
    i have solved it with your suggestions.....
    regards:
    raghunadhs

  11. #11
    New Member
    Join Date
    Apr 2010
    Location
    Armenia
    Posts
    3

    Re: [RESOLVED] how to find the length of array in v.b 6.0

    UBound - LBound + 1

  12. #12
    Registered User Mithun Thangachen's Avatar
    Join Date
    Dec 2015
    Posts
    1

    Re: [RESOLVED] how to find the length of array in v.b 6.0

    Can any one help me to save data in JSON format

  13. #13
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [RESOLVED] how to find the length of array in v.b 6.0

    You should start your own thread instead of piggy-backing on another that isn't even related to your question. The codebank has a JSON project that I'm sure describes how to read/write data
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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