好的,所以我对VB6不是很熟悉,但是我想看一个数组是否包含一个值。这就是我所拥有的,但它给我带来了错误。可能是“passedValue”是错误类型的问题,但我不这么认为。
Dim transCodes As Variant
transCodes = Array(40, 41, 42, 43)
If (transCodes.Contains("passedValue")) Then
*Do Stuff*
End If
任何帮助都会非常感激!
更新
未能纠正我的语法,你能给我一个示例,我可能会用它来确保“passedValue”属于正确的类型吗?
更新我的更新
VB6中没有'Contains'方法吗?做这个简单任务的其他方法吗?
答案 0 :(得分:11)
VB6在数组上没有本地Contains
方法。
您最好的选择是依次检查每个项目的数组:
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
替代方案包括使用集合并尝试根据其值来检索项目,但对于这个简单的案例,这是更多的工作。
答案 1 :(得分:4)
如果您只需要一行,可以使用以下代码检查字符串数组是否包含项目(没有循环):
' 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
答案 2 :(得分:1)
最后基于@Mostafa Anssary
我使用了这个功能
Function instrSplit(ByVal searchEstado As String, ByVal arrayEstados As String)
instrSplit = 0
instrSplit = InStr(1, " " & Join(Split(arrayEstados, ","), " ") & " ", _
" " & searchEstado & " ")
End Function
我打电话
found = instrSplit(mySearchValue, arrayValues)
例如
arrayValues = "8, 64, 72, 1520"
mySearchValue = "8"
found = instrSplit(mySearchValue, arrayValues)
答案 3 :(得分:1)
我将Deanna的答案包装成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