【问题标题】:How to build a 'SuperDictionary' custom object / Excel-VBA [closed]如何构建“SuperDictionary”自定义对象/Excel-VBA [关闭]
【发布时间】:2014-04-21 18:52:23
【问题描述】:

作为 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

【问题讨论】:

  • 这对 StackOverflow 来说不是一个真正的问题。它可能适合codereview.stackexchange.com,但这里不适合。
  • 本课程打算处理哪些“小问题”?例如,尚不清楚为什么您将数字键强制转换为 Long。
  • @RBarryYoung。谢了。会坚持我的主要问题。
  • @TimWilliams。我什至还不知道所有问题。但我相信这个类将成为我在各种项目中使用的类,所以我希望有一种通用的方法。
  • 为分享您的课程点赞!

标签: excel dictionary custom-object vba


【解决方案1】:

据我所知,非常好看的课程(尽管您的 VBA 课程构建技能明显比我的高级)。我可能建议的是,如果您精通 .NET,则在 Visual Studio 中将其重新创建为可移植类库,以便您可以利用 .NET 框架的强大功能。

  1. 在 Visual Studio 中创建一个新的“类库”。
  2. 将System 和System.Runtime.InteropServices 导入您的新课程。
  3. 将类包装在命名空间中。
  4. 为您的属性、方法等创建接口。
  5. 转到“编译”设置并单击“注册 COM 互操作”。
  6. 构建项目 - 这会在项目的 BIN 文件夹中创建一个 .TLB 文件。
  7. 在 VBA 开发人员环境中添加 .TLB 文件作为参考。

下面是 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

你可以用 .NET 做什么而 VBA 不能?

这是一个很好的问题,我很高兴你问。显然,您可以做的比我在这里演示的要多得多。举例来说,假设您想集成一些现在所有酷孩子都在使用的花哨的新 Web 服务。无论您是使用 WSDL 文件与 Web 服务进行通信,还是您自己的自定义 REST 方法,.NET 框架的类与 Visual Studio 2012 开发人员环境中的大量工具相结合,使得使用 .NET 比 VBA 更可取.使用上面概述的技术,您可以为这些 Web 服务创建一个包装类,该类使用自定义方法来执行所有必要的操作,然后将 VBA 兼容的对象和/或数据类型返回给 VBA。好多了,不是吗?

更不用说,您创建的库还可以与其他平台兼容,例如 ASP.NET、Windows Phones、Silverlight、Xbox 等。


我使用的一些有用的链接(我会在找到它们时添加更多):

【讨论】:

  • 欣赏,但我的客户希望在 VBA 中使用它。如果可以的话,我想我会在 python 中。
猜你喜欢
  • 2021-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-13
  • 2014-03-31
  • 2020-06-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多