1

I m trying to find out array index using visual basic. I tried some code with VB.Net and getting correct output. Below is the code I am using,

Dim FindThisString as String="EFGH"
Dim MyArray() As String={"ABCD","EFGH","IJKLM"}
For Each Str As String In MyArray
If Str.Contains(FindThisString) Then
    MsgBox(Str.IndexOf(FindThisString))
End If 
Next

Now I want to try the same method with VB 6.0. I am using Instr function but it is giving me string index in entire string and I am looking for array index i.e. index of string "EFGH" in MyArray.

Here is the VB6 code I'm trying:

Dim MyString as String 
Dim str as Variant 
MyString="ABCD/EFGH/IJKLM" 
Dim MyArray() as String 
MyArray = split(MyString,"/") 
Dim inIndex as Integer 
For Each Str In MyArray 
   inIndex= Instr(str,"EFGH") 
   MsgBox inIndex 
Next
3
  • Show us your VB6 code also. You'll have to iterate over the array in VB6 too. Commented Dec 19, 2014 at 7:38
  • Dim MyString as String Dim str as Variant MyString="ABCD/EFGH/IJKLM" Dim MyArray() as String MyArray = split(MyString,"/") Dim inIndex as Integer For Each Str In MyArray inIndex= Instr(str,"EFGH") MsgBox inIndex Next Commented Dec 19, 2014 at 8:02
  • @idstam : I a trying above VB6 code Commented Dec 19, 2014 at 8:03

2 Answers 2

1

You would basically use the same algorithm:

  • Loop through the array (you'll need to use a Variant as the loop variable for VB Classic For Each),

  • verify if the array entry contains the substring in question (you need to use InStr here, since VB Classic does not have String.Contains),

  • return the index (which you already determined with InStr).

The implementation is left as an exercise.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi Heinzi.. I have gone thru your suggestion. But your solution is mostly for string and not for array of string. As Instr in vb6 accept two parameters i.e. Instr(Stringinto, "Stringtosearch"). But i am looking for array index.
@Mahesh: Ah, but your VB.NET example code does not return the array index: It returns the string index!
That What I meant to say here. Here is the VB6 code I m trying.. Dim MyString as String Dim str as Variant MyString="ABCD/EFGH/IJKLM" Dim MyArray() as String MyArray = split(MyString,"/") Dim inIndex as Integer For Each Str In MyArray inIndex= Instr(str,"EFGH") MsgBox inIndex Next
@Mahesh: In that case, use a regular For loop instead of a For Each loop and return the loop counter.
Thanks for your suggestion. will try the same and will let you know.
0
Function IndexOf(ByRef arr() As String, ByVal str As String) As Integer
    Dim joinedStr As String
    Dim strIndex As Integer
    joinedStr = "|" & Join(arr, "|")
    strIndex = InStr(1, joinedStr, str)
    If strIndex = 0 Then
        IndexOf = -1
        Exit Function
    End If
    joinedStr = Mid(joinedStr, 1, strIndex - 1)
    IndexOf = UBound(Split(joinedStr, "|")) - 1
End Function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.