Link to home
Start Free TrialLog in
Avatar of ipjyo
ipjyoFlag for United States of America

asked on

How to find the length of an array in VB6?

hi,

I am trying to find the number of elements in an array. and then print each value in the array.
Can anybody please suggest how to do this? The following is not working. Thanks.


Private Sub Form_Load()
    Dim strarray() As String
    strarray(0) = "1"
    strarray(1) = "2"
       
    For i = 1 To Len(strarray())
        MsgBox (strarray(i))
    Next
End Sub
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

SOLUTION
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ipjyo

ASKER

Thanks for the suggestion. Now the below code is working. I had to include "ReDim" statement also.
But if I dont know how many values this array is going to hold, how can I define the upper bound in the ReDim statement. Is there any alternative way? Thanks.

Private Sub Form_Load()
    Dim strarray() As String
       
    ReDim strarray(2) As String
   
    strarray(0) = "1"
    strarray(1) = "2"
           
    For i = 0 To UBound(strarray) - 1
        MsgBox (strarray(i))
    Next
End Sub
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
this should work, tell me if you get errors
Private Sub Form_Load()
    Dim strarray() As String
    strarray(0) = "1"
    strarray(1) = "2"
       
    For i = 1 To ubound(strarray)
        MsgBox (strarray(i))
    Next
End Sub

Open in new window

Avatar of ipjyo

ASKER

hi,

did nt work for me.
I got the below error.

Run-time error '9':

Subscript out of range

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Is this working for you?
Private Sub Form_Load()
    Dim strarray(2) As String
    strarray(0) = "1"
    strarray(1) = "2"
       
    For i = 0 To UBound(strarray) - 1
        MsgBox (strarray(i))
    Next
End Sub

Open in new window

Avatar of ipjyo

ASKER

Thank you very much everybody for the clarifications. In my case, I have to use arrays for some reason. I can't use Collection object.
It looks like I should always specify the Upper Bound of the array to be able to loop through the array.

I am trying to get the attached code working.
It gives me "Subscript out of range error" when the array strIDs() is empty and does not have any values.
Should I specify the upper bound while declaring the array as follows.

Dim strIDs(50) As String

Thank you.
Dim strIDs() As String

strIDs() = GetIDs  'returns an array of strings and the array can be empty sometimes

bIDExists = IDExists(CurrentID, strIDs())     'returns true if CurrentID exists in strIDs()


Private Function IDExists(strCurrentID As String, _
                                IDs() As String) As Boolean
    Dim i As Integer
    Dim bExists As Boolean
    bExists = False
    
    For i = 0 To UBound(IDs)
        If strCurrentID = IDs(i) Then
            bExists = True
            Exit For
        End If
    Next i
    
    IDExists = bExists

End Function

Open in new window

Ark was demonstrating in his comment back here:
https://www.experts-exchange.com/questions/26442863/How-to-find-the-length-of-an-array-in-VB6.html#33574084

...that his PureUBound() function will work for empty arrays and will return -1 in those cases.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ipjyo

ASKER

Thanks very much. It is working for me when I tested it in a form.
But when I tried to place this line in my class module, it says

Run-time error '453':
Can't find DLL entry point SafeArrayGetDim in oleaut32

Private Declare Function SafeArrayGetDim Lib "oleaut32" (ByRef saArray() As Any) As Long

I have the function as follows.

Private Function IDExists(strCurrentID As String, _
                                IDs() As String) As Boolean
    Dim i As Integer
    Dim bExists As Boolean
    bExists = False
   
If SafeArrayGetDim(IDs) Then

    For i = 0 To UBound(IDs)
        If strCurrentID = IDs(i) Then
            bExists = True
            Exit For
        End If
    Next i
Else
    bExists = False
End If
   
    IDExists = bExists

End Function
Avatar of ipjyo

ASKER

Sorry...It was working fine. I tried to change the name of the function SafeArrayGetDim to something else. Then it did not work. But I tried with the same function name "SafeArrayGetDim" and it is working great.

Thanks.
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

You can change the name, but you also must specify the Alias member in the decleration if you want to to change the name.

Private Declare Function SafeArrayAnyNameYouWant Lib "oleaut32" Alias "SafeArrayGetDim" (ByRef saArray() As Any) As Long

Open in new window

Ark Said:Though it seems to be a newbie question, it's not so trivial - VB6 doesn't recognized UBound for undimmed arrays:

That's correct... that is why I use the trick of ALWAYS doing a ReDim on any undimmed arrays at the start of the program (or in a class/form Initialize event) with...
  ReDim m_MyArray( -1 to -1 )
..., that way from the moment the application starts, the array actually has been dimmed so that a UBound command won't blow up, but I can test if my logic (post initilization) has ReDimmed the array by testing...
  if UBound( m_MyArray ) = -1 then
...

Sub Class_Initialize
  ReDim m_MyArray( -1 to -1)
End Sub

...that way, if I want to test if the code that REALLY is supposed to ReDim the array has executed or not, I can use UBound, and if the result is -1, then I know the array hasn't LOGICALLY been allocated.
Arrays are zero based by default except you define the lower bound

this is the best ,method if  you don't know the lower bound and upper bound of the array

try this


Private Sub Form_Load()
    Dim strarray() As String
    strarray(0) = "1"
    strarray(1) = "2"
       
    For i = lbound(strarray) To ubound(strarray)
        MsgBox (strarray(i))
    Next
End Sub

Open in new window

>>Arrays are zero based by default except <b>YOU</b> define the lower bound
this is the best ,method if  <b>YOU</> don't know the lower bound and upper bound of the array<<
:)
Avatar of ipjyo

ASKER

Thank you very much for the additional clarifications.