excel - 实现 scripting.dictionary 项
问题描述
我正在尝试创建一个继承Scripting.Dictionnary
以创建具有类型限制键和项目的哈希表的类。
我遇到的问题是我没有找到任何关于如何实现它的文档,并且我有一条错误消息告诉我必须实现 Item 到接口字典。
这是我班级的原型:
Option Explicit
Implements Dictionary
Public Sub Add(nom As String, jour As Date, temps As Integer)
Supplier.Add nom, Array(jour, temps)
End Sub
Public Property Get Item(Key As String) As Array
Item = Supplier.Item(Key)
End Property
Public Property Set Item(Key As String, jour As Date, temps As Integer)
Set Supplier.Item(Key) = Array(jour, temps)
End Property
我应该如何实施Item
以使其工作?这是实现我想要的好方法吗?
解决方案
您的既定目标是实现强类型字典。为了实现这个目标,我不会实现接口。相反,我会将 Dictionary 包装在一个类中,并通过使用另一个类来实现强类型:
供应商类
Option Explicit
Private Supplier As Dictionary
Private Sub Class_Initialize()
Set Supplier = New Dictionary
End Sub
Public Sub Add(Key As String, Item As SupplierItem)
Supplier.Add Key, Item
End Sub
Public Property Get Item(Key As String) As SupplierItem
Set Item = Supplier.Item(Key)
End Property
Public Property Set Item(Key As String, Value As SupplierItem)
Set Supplier.Item(Key) = Value
End Property
供应商项目类
Option Explicit
Public jour As Date
Public temps As Integer
测试逻辑
Option Explicit
Public Sub Test()
Dim s As Supplier
Dim si As SupplierItem
Set s = New Supplier
Set si = New SupplierItem
si.jour = Now
si.temps = 3
s.Add "Key1", si
Debug.Print s.Item("Key1").temps
Set si = New SupplierItem
si.jour = Now
si.temps = 4
Set s.Item("Key1") = si
Debug.Print s.Item("Key1").temps
End Sub
推荐阅读
- rest - 在服务器内部集成 MQTT 代理
- swift - 在通用 swift 对象中约束 PAT
- ruby-on-rails - 如何为 Rails 控制器中的嵌套项添加默认值?
- powershell - 如何在 Powershell 中模拟“GoSub / Return”
- angular - 带有 Font Awesome 图标的 Ag-grid cellRenderer
- html - CSS类未应用于HTML图像(django)
- c# - 如何使用 JSON.NET 将 JSON 数组中的对象名称用作属性值?
- python - tensorflow:只能在 val_acc 可用的情况下保存最佳模型,跳过
- java - 如果数组在 Java 中已满,如何将值传递给类中的另一个方法?
- android - 带有 Dagger2 的多模块 android 应用程序,而不在功能模块中使用 `kapt`