如何检查集合中是否存在某个键

4

我想在Visual Basic 6.0中检查集合变量是否包含该键。 以下是我拥有的集合变量:

pcolFields As Collection

我想检查它是否包含字段事件代码(Event_Code)。我按以下方式进行操作,但对我没有起作用。

    If IsMissing(pcolFields("Event_Code")) = False Then
        'Do Something
    End If

你也可以在这里获得一些帮助:https://dev59.com/9HVD5IYBdhLWcg3wQJKT。 - Brian Hooper
为了实现最快速的实现,您需要使用自定义的typelib并调整ICollection接口。 - wqw
4个回答

3

以下是一个使用try-catch的示例解决方案:

Private Function IsMissing(col As Collection, field As String)
On Error GoTo IsMissingError
    Dim val As Variant
    val = col(field)
    IsMissing = False
    Exit Function
IsMissingError:
    IsMissing = True
End Function

使用方法如下:

Private Sub Form_Load()
    Dim x As New Collection
    x.Add "val1", "key1"

    Dim testkey As String
    testkey = "key2"
    If IsMissing(x, testkey) Then
        Debug.Print "Key is Missing"
    Else
        Debug.Print "Val is " + x(testkey)
    End If

    Exit Sub
End Sub

你可以尝试实现或继承集合并添加一个“has”函数,来解决这个问题。

Collection 对象很有用,但比较原始,没有像您需要的那样好用的功能。基本解决方案如此 - 尝试引用和使用错误检测作为 try-catch 结构。如果要检测成员(记录)是否存在,则同样适用。Dictionary 对象有时是更好的选择,但如果您只需要回答问题,则 try-catch 模式是最佳解决方案。 - Vanquished Wombat
1
小心 Scripting.Dictionary 的性能陷阱。该类的几个成员会提取并返回整个键或项内容的数组。除非 Count 相当小,否则这可能会导致性能问题。 - Bob77

1
集合在需要检查存在性时并不实用,但在迭代时很有用。然而,集合是变量的集合,因此比类型化变量 inherently 更慢。
几乎在每种情况下,使用类型化数组更有用(也更优化)。如果您需要拥有键控集合,则应使用字典对象。
以下是使用类型化数组的一些常见示例:
Dim my_array() As Long ' Or whichever type you need
Dim my_array_size As Long
Dim index As Long
Dim position As Long

' Add new item (push)
ReDim Preserve my_array(my_array_size)
my_array(my_array_size) = 123456 ' something to add
my_array_size = my_array_size + 1

' Remove item (pop)
my_array_size = my_array_size - 1
If my_array_size > 0 Then
    ReDim Preserve my_array(my_array_size - 1)
Else
    Erase my_array
End If

' Remove item (any position)
position = 3 'item to remove
For index = position To my_array_size - 2
    my_array(index) = my_array(index + 1)
Next
my_array_size = my_array_size - 1
ReDim Preserve my_array(my_array_size - 1)

' Insert item (any position)
ReDim Preserve my_array(my_array_size)
my_array_size = my_array_size + 1
For index = my_array_size - 1 To position + 1 Step -1
    my_array(index) = my_array(index - 1)
Next
my_array(position) = 123456 ' something to insert

' Find item
For index = 0 To my_array_size - 1
    If my_array(index) = 123456 Then
        Exit For
    End If
Next
If index < my_array_size Then
    'found, position is in index
Else
    'not found
End If

虽然代码看起来很多,但速度更快。Intellisense也能正常工作,这是一个好处。唯一的注意点是如果数据集非常大,那么redim会变得很慢,你需要使用稍微不同的技巧。
你也可以使用字典,确保在项目中包含Microsoft Scripting Runtime引用。
Dim dict As New Dictionary
Dim value As Long

dict.Add "somekey", 123456

dict.Remove "somekey"

value = dict.Item("somekey")

If dict.Exists("somekey") Then
    ' found!
Else
    ' not found
End If

像集合一样,字典只是保存了一堆变量,因此可以保存对象等。


1
我们可以将以下代码转换为vb.net代码:
如果Collection.ContainsKey(KeyString) Then
'编写代码
End if
Collection是Dictionary的变量,KeyString是我们需要在集合中查找的键字符串。

0

如果集合中包含对象而不是基本类型,efkah的方法将失败。这里有一个小调整:

'Test if a key is available in a collection
Public Function HasKey(coll As Collection, strKey As String) As Boolean
    On Error GoTo IsMissingError
        Dim val As Variant
'        val = coll(strKey)
        HasKey = IsObject(coll(strKey))
        HasKey = True
        On Error GoTo 0
        Exit Function
IsMissingError:
        HasKey = False
        On Error GoTo 0
End Function

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接