[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
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
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.
Re: how to find the length of array in v.b 6.0
To find only "populated" items you'd have to loop through array - there is no any other way.
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 <> ""
Re: how to find the length of array in v.b 6.0
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.
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.
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.
Re: how to find the length of array in v.b 6.0
Thanks all,
i have solved it with your suggestions.....
regards:
raghunadhs
Re: [RESOLVED] how to find the length of array in v.b 6.0
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
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