1

vba の辞書のヘビー ユーザーとして、メイン コードを処理したくない多くの小さな問題を処理する「スーパー辞書」クラスを作成すると便利であることがわかりました。以下は、この「超辞書」カスタム オブジェクトのドラフトです。

これは良い考えですか?このアプローチは、予期しない方法で辞書のパフォーマンスに影響を与える可能性がありますか? (たとえば、私のGet Itemメソッドは高価ですか? - 私はよく使います)

事前に!

Public pDictionary As Object

Private Sub Class_Initialize()
    Set pDictionary = CreateObject("Scripting.Dictionary")
End Sub

Private Sub Class_Terminate()
    If Not pDictionary Is Nothing Then Set pDictionary = Nothing
End Sub

Public Property Get GetItem(Key As Variant) As Variant:
    If VarType(pDictionary.Items()(1)) = vbObject Then
        Set GetItem = pDictionary(Key)
    Else
        GetItem = pDictionary(Key)
    End If
End Property

Public Property Get GetItems() As Variant:
    Dim tmpArray() As Variant, i As Integer
    If Not pDictionary.Count = 0 Then
        ReDim tmpArray(pDictionary.Count - 1)
        For i = 0 To pDictionary.Count - 1
            If VarType(pDictionary.Items()(i)) = vbObject Then Set tmpArray(i) =pDictionary.Items()(i)
            If Not VarType(pDictionary.Items()(i)) = vbObject Then tmpArray(i) =pDictionary.Items()(i)
        Next i
    Else
       ReDim tmpArray(0)
    End If
      GetItems = tmpArray
End Property

Public Property Get GetKeys() As Variant:
    GetKeys = pDictionary.Keys
End Property

Public Property Get Count() As Integer:
    Count = pDictionary.Count
End Property

Public Property Get Exists(Key As Variant) As Boolean:
    If IsNumeric(Key) Then Exists = pDictionary.Exists(CLng(Key))
    If Not IsNumeric(Key) Then Exists = pDictionary.Exists(Key)
End Property

Public Sub Add(Key As Variant, Item As Variant):
     If IsNumeric(Key) Then pDictionary.Add CLng(Key), Item
     If Not IsNumeric(Key) Then pDictionary.Add Key, Item
End Sub

Public Sub AddorSkip(Key As Variant, Item As Variant):
    If IsNumeric(Key) Then
        If Not pDictionary.Exists(CLng(Key)) Then pDictionary.Add CLng(Key), Item
    Else
        If Not pDictionary.Exists(Key) Then pDictionary.Add Key, Item
    End If
End Sub

Public Sub AddorError(Key As Variant, Item As Variant):
    If IsNumeric(Key) Then
        If Not pDictionary.Exists(CLng(Key)) Then
            pDictionary.Add CLng(Key), Item
        Else
            MsgBox ("Double entry in Dictionary: " & Key & " already exists."): End
        End If
    Else
        If Not pDictionary.Exists(Key) Then
            pDictionary.Add Key, Item
        Else
            MsgBox ("Double entry in Dictionary: " & Key & " already exists"): End
        End If
    End If
End Sub

Public Sub Remove(Key As Variant):
    If IsNumeric(Key) Then
        pDictionary.Remove (CLng(Key))
    Else
        pDictionary.Remove (Key)
    End If
End Sub
4

1 に答える 1