English 中文(简体)
在数组中使用包含
原标题:Using Contains on an Array
  • 时间:2012-05-23 13:36:32
  •  标签:
  • arrays
  • vb6

好吧,我对 VB6 不太熟悉, 但我想看看一个数组是否包含一个值。 这就是我拥有的, 但它给我造成了一个错误。 可能是“ 过价” 类型错误的问题, 但我不这么认为 。

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

任何帮助都会非常感激!

<强> UPDATE

未能纠正我的语法, 你能给我举个例子吗? 我可以用来保证"通过价值" 是正确的类型?

< 强势 > 播放我的更新 < /强 >

VB6 中没有包含方法吗? 还有其它方法可以做这个简单的任务吗?

最佳回答

VB6 在阵列上没有本地的 包含 方法。

您的最佳选择是行走阵列, 逐项检查 :

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

替代办法包括利用收藏品,并试图根据价值对物品进行修复,但对于这一简单案例来说,这要多得多。

问题回答

如果您只想要一条线,您可以检查字符串数组是否包含一个包含以下代码的项目( 没有循环) :

  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

这是从一个""http://www.devx.com/vb2themax/Tip/ 18364" rel="no follow" >Devx tip 上摘取的

我用 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

最后基于 @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)




相关问题
Prevent windows from queuing shellexecute requests

Win.ShellExecute 0, "open", "C:dirprogram.exe", "arguments", vbNullString, SW_SHOWNORMAL Win.ShellExecute 0, "open", "http://www.google.com", vbNullString, vbNullString, SW_SHOWNORMAL I want google....

Why is My Loop Only Deleting One File?

Using VB6 In a folder, i have n number of files, i want to delete a 0 kb files code Dim filename5 As String filename5 = Dir$(txtsourcedatabasefile & "*_*", vbDirectory) MsgBox filename5 Do ...

How to check the filesize?

Using VB6 I have the text file with different sizes, so i want to delete the file where filesize = 0 kb. How to make a vb6 code for deleting the 0 kb files. Need vb6 code Help

File Rename problem?

I m using VB6 and I have a folder where I have n number of files. I want to change the file extension to .txt. I used the code below to change the extension of all .fin files to .txt. Dim filename1 ...

Error 20728-F while in using Crystal Reports in VB6

I m using Crystal Reports in my VB6 project, but I m facing error while loading the report in crystalreport1.action=1; Please give me some solution for this problem. It is showing the error as Error ...

DllRegisterServer entry point was not found

When running my vb6 application I am getting error like, runtime error 53 : file not found: rscomclNoMsg.dll then i tried to register that dll from cmd line using regsvr32. Then I am getting ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签