如何检查密钥是否存在于集合中 [英] How to check the key is exists in collection or not

查看:29
本文介绍了如何检查密钥是否存在于集合中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to check collection variable contains the key or not in visual basic 6.0 Below is the collection variable I am having

pcolFields As Collection

我想检查它是否包含字段 Event_Code.我正在按如下方式执行此操作,但对我不起作用.

and I want to check whether it contains the field Event_Code. I am doing this as below but it not worked for me.

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

推荐答案

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

Collections are not useful if you need to check for existence, but they're useful for iteration. However, collections are sets of Variants and so are inherently slower than typed variables.

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

In nearly every case it's more useful (and more optimal) to use a typed array. If you need to have a keyed collection you should use the Dictionary object.

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

Some examples of general ways of using typed arrays:

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 就会开始变慢,您必须使用稍微不同的技术.

Whilst it may seem like a lot code. It is way faster. Intellisense will also work, which is a bonus. The only caveat is if you have very large data sets, then redim starts to get slow and you have to use slightly different techniques.

您也可以使用字典,确保在您的项目中包含 Microsoft Scripting Runtime 参考:

You can also use a Dictionary, be sure to include the Microsoft Scripting Runtime reference in your project:

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

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

Dictionaries like collections just hold a bunch of Variants, so can hold objects etc.

这篇关于如何检查密钥是否存在于集合中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆