我要檢查集合變量包含密鑰或沒有的Visual Basic 6.0 下面是我有如何檢查的關鍵是存在於收集或不
pcolFields As Collection
收集變量,我要檢查是否包含字段Event_Code。我正在做這個如下,但它不適合我。
If IsMissing(pcolFields("Event_Code")) = False Then
'Do Something
End If
我要檢查集合變量包含密鑰或沒有的Visual Basic 6.0 下面是我有如何檢查的關鍵是存在於收集或不
pcolFields As Collection
收集變量,我要檢查是否包含字段Event_Code。我正在做這個如下,但它不適合我。
If IsMissing(pcolFields("Event_Code")) = False Then
'Do Something
End If
如果您需要檢查是否存在,則集合無用,但它們對迭代很有用。但是,集合是變體的集合,因此本質上比類型變量慢。
在幾乎所有情況下,使用類型化數組都更有用(也更優化)。如果你需要有一個鍵控集合,你應該使用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
雖然它可能看起來像很多代碼。它速度更快。智能感知也將起作用,這是一項獎勵。唯一需要注意的是,如果你有非常大的數據集,那麼REDIM開始變慢,你必須使用稍微不同的技術。
你也可以使用一個字典,一定要在項目中包含的Microsoft腳本運行時參考:
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
字典喜歡收藏只是抱着一堆變的,所以可以容納對象等
下面是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」功能
集合對象是有用的,但原始的,沒有很好的功能,如你需要。基本的解決方案是根據這個答案 - 嘗試引用並使用錯誤檢測作爲try catch結構。檢測成員(記錄)是否存在也是如此。 Dictionary對象有時候是一個更好的選擇,但如果你需要的只是回答這個問題,那麼try-catch模式就是最佳的解決方案。 –
請注意Scripting.Dictionary的性能死亡陷阱。該類的幾個成員提取並返回整個鍵或項目內容的數組。這些可能是一個殺手,除非伯爵相當小。 – Bob77
你也可以得到一些幫助[這裏](http://stackoverflow.com/questions/40651/check-if-a-record-exists-in-a-vb6-collection)。 –
[檢查一個記錄是否存在於VB6集合中?]可能的重複(http://stackoverflow.com/questions/40651/check-if-a-record-exists-in-a-vb6-collection) –
對於[儘可能快的實現](http://www.vbforums.com/showthread.php?822115-searching-through-a-collection),你需要一個定製的typelib,並調整了'ICollection'接口。 – wqw