【问题标题】:How to check the key is exists in collection or not如何检查密钥是否存在于集合中
【发布时间】:2021-06-22 23:06:01
【问题描述】:

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

pcolFields As Collection

我想检查它是否包含字段 Event_Code。我正在这样做,但它对我不起作用。

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

【问题讨论】:

标签: vb6


【解决方案1】:

这是一个使用 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

您也可以尝试实现或子类化集合并添加“具有”功能

【讨论】:

  • Collection 对象很有用,但很原始,没有您需要的好功能。基本解决方案是按照这个答案 - 尝试引用并使用错误检测作为尝试捕获构造。检测成员(记录)是否存在也是如此。 Dictionary 对象有时是更好的选择,但如果您只需要回答问题,那么 try-catch 模式是最佳解决方案。
  • 当心 Scripting.Dictionary 的性能陷阱。此类的一些成员提取并返回整个键或项目内容的数组。除非伯爵相当小,否则它们可能会成为杀手。
【解决方案2】:

如果您需要检查是否存在,集合没有用,但它们对于迭代很有用。但是,集合是变体的集合,因此本质上比类型变量慢。

几乎在所有情况下,使用类型化数组都更有用(也更优化)。如果你需要一个键控集合,你应该使用 Dictionary 对象。

使用类型化数组的一般方法的一些示例:

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

像集合这样的字典只保存一堆变体,所以可以保存对象等。

【讨论】:

    【解决方案3】:

    我们可以将以下代码签入vb.net代码

    如果 Collection.ContainsKey(KeyString) 那么

    '写代码

    如果结束

    Collection 是 Dictionary 的变量,KeyString 是我们需要在 collection 中找到的关键字符串

    【讨论】:

      【解决方案4】:

      如果 Collection 包含对象而不是原始类型,则来自 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
      

      【讨论】:

        猜你喜欢
        • 2020-08-19
        • 2011-06-05
        • 2018-04-20
        • 1970-01-01
        • 2013-07-03
        • 2019-01-21
        • 1970-01-01
        • 2014-09-14
        相关资源
        最近更新 更多