作为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
答案 0 :(得分:1)
据我所知,非常好看的课程(虽然你的VBA级别的建设技能显然比我的更先进)。我可能建议的是,如果您精通.NET,那么在Visual Studio中将其作为可移植类库重新创建,以便您可以利用.NET框架的强大功能。
System
和System.Runtime.InteropServices
导入新课程。以下是VB.net代码的示例:
Option Strict On
Imports System
Imports System.Runtime.InteropServices
Namespace SuperDictionary
' Interface with members of the Super-Dictionary library exposed in the TLB.
Public Interface ISuperDictionaryInterface
ReadOnly Property MyListOfTypeStringProperty(ByVal index As Integer) As String
Function MyMethod(ByVal someValue as Variant) as Boolean
End Interface
<ClassInterface(ClassInterfaceType.None)>
Public Class SuperDictionary: Implements ISuperDictionaryInterface
Public Function MyMethod(ByVal someValue as Variant) As Boolean Implements ISuperDictionaryInterface.MyMethod
'========================
'Your code here
'========================
End Function
Private _MyListOfTypeStringProperty As List(Of String)
Public ReadOnly Property MyListOfTypeStringProperty(ByVal index as Integer) As String Implements ISuperDictionaryInterface.MyListOfTypeStringProperty
Get
Return _MyListOfTypeString(index)
End Get
End Property
End Class
End Namespace
这是一个很好的问题,我很高兴你问。显然你可以做的比我在这里展示的要多得多。让我们说,为了举个例子,您希望整合一些新鲜的 Web服务所有酷孩子们正在使用这些天。无论您是使用WSDL文件与Web服务进行通信,还是使用自己的自定义REST方法,.NET框架的类与Visual Studio 2012开发人员环境中的大量工具相结合,使.NET更受欢迎比VBA。使用上面概述的技术,您可以为这些Web服务创建一个包装类,它使用自定义方法执行所有必要的操作,然后将VBA兼容的对象和/或数据类型返回给VBA。好多了,不是吗?
更不用说,您创建的库也可以与其他平台兼容,如ASP.NET,Windows Phone,Silverlight,Xbox等。
我使用过的一些有用的链接(我会在找到它们时添加更多内容):