Using Contains on an Array

Good, so I'm not very familiar with VB6, but I'm trying to figure out if the array contains a value. This is what I have, but it causes an error for me. Maybe the “passing shaft” problem is the wrong type, but I don't think so.

    Dim transCodes As Variant
    transCodes = Array(40, 41, 42, 43)
    If (transCodes.Contains("passedValue")) Then
    *Do Stuff*
    End If

Any help would be really appreciated!

UPDATE

My syntax failed to fix, could you give me an example for cast / convert, which I could use to provide the correct type of "receivedValue"?

UPDATE MY UPDATES

So, there is no “Contains” method in VB6? Any other ways to accomplish this simple task?

+5
source share
4 answers

VB6 does not have a built-in method Containson arrays.

- , :

Found = False
For Index = LBound(transCodes) To UBound(transCodes )
  If transCodes(Index) = PassedValue Then
    Found = True
    Exit For
  End If
Next

If Found Then
  'Do stuff
  'Index will contain the location it was found
End If

, .

+9

, , ( ) :

' ARR is an array of string, SEARCH is the value to be searched
Found = InStr(1, vbNullChar & Join(arr, vbNullChar) & vbNullChar, _
vbNullChar & search & vbNullChar) > 0

Devx tip

+3

Finally, based on @Mostafa Anssary

I used this function

    Function instrSplit(ByVal searchEstado As String, ByVal arrayEstados As String)

       instrSplit = 0

       instrSplit = InStr(1, " " & Join(Split(arrayEstados, ","), " ") & " ", _
                " " & searchEstado & " ")

    End Function

And i'm calling

found = instrSplit(mySearchValue, arrayValues)

for instance

arrayValues = "8, 64, 72, 1520"
mySearchValue  = "8"


found = instrSplit(mySearchValue, arrayValues)
+1
source

I wrapped Deanna as a function of VB6:

Public Function StringArrayContains(ByRef arrString() As Boolean, PassedValue) As String
   Dim Found As Boolean
   Found = False
   Dim index  As Integer
    For Index = LBound(arrString) To UBound(arrString)
      If arrString(Index) = PassedValue Then
        Found = True
        Exit For
      End If
    Next
    StringArrayContains = Found
End Function
0
source

All Articles