gpt4 book ai didi

vba - 将字典放入类中

转载 作者:行者123 更新时间:2023-12-02 05:40:20 26 4
gpt4 key购买 nike

我得到了关于如何制作通用类模块的答案:Class "let" stuck in infinite loop

我正在尝试将其应用到我的类中的字典中。

我的类(class)模块:

Option Explicit

Private Type categories
Temp As scripting.Dictionary
Humid As scripting.Dictionary
Wind As scripting.Dictionary
End Type

Private this As categories

Public Sub Initialize()
Set this.Temp = New scripting.Dictionary
Set this.Humid = New scripting.Dictionary
Set this.Wind = New scripting.Dictionary
End Sub

Public Property Get Temp(ByVal HourIndex As Long) As Double
Temp = this.Temp(HourIndex)
End Property

Public Property Let Temp(ByVal HourIndex As Long, ByVal Value As Double)
this.Temp(HourIndex) = Value
End Property

Public Property Get Humid(ByVal HourIndex As Long) As Double
Humid = this.Humid(HourIndex)
End Property

Public Property Let Humid(ByVal HourIndex As Long, ByVal Value As Double)
this.Humid(HourIndex) = Value
End Property

Public Property Get Wind(ByVal HourIndex As Long) As Double
Wind = this.Wind(HourIndex)
End Property

Public Property Let Wind(ByVal HourIndex As Long, ByVal Value As Double)
this.Wind(HourIndex) = Value
End Property

我尝试在立即窗口中使用set tester = new WeatherData(模块的名称)和Initialize对此进行测试。那没有用。

然后我修改了初始化:

Public Sub Initialize(ByVal variable As categories)
Set variable.Temp = New scripting.Dictionary
Set variable.Humid = New scripting.Dictionary
Set variable.Wind = New scripting.Dictionary
End Sub

并输入初始化测试器,但这也不起作用(“编译错误:子或函数未定义”)。

如何将三个字典放入一个类模块中?

以下内容并没有解决问题,但它确实绕过了这个问题,以至于我不必承认它:

Option Explicit

Private Type categories
Temp(23) As Double
Humid(23) As Double
wind(23) As Double
End Type

Private this As categories

Public Property Get Temp(ByVal HourIndex As Long) As Double
Temp = this.Temp(HourIndex)
End Property

Public Property Let Temp(ByVal HourIndex As Long, ByVal Value As Double)
this.Temp(HourIndex) = Value
End Property

Public Property Get Humid(ByVal HourIndex As Long) As Double
Humid = this.Humid(HourIndex)
End Property

Public Property Let Humid(ByVal HourIndex As Long, ByVal Value As Double)
this.Humid(HourIndex) = Value
End Property

Public Property Get wind(ByVal HourIndex As Long) As Double
wind = this.WindChill(HourIndex)
End Property

Public Property Let wind(ByVal HourIndex As Long, ByVal Value As Double)
this.wind(HourIndex) = Value
End Property

tl;dr:使用数组代替字典,并完全删除初始化。你的“ key ”别无选择,只能是数字,但它有效。我有兴趣了解实际的解决方案,但具体问题已解决。

最佳答案

似乎您想要实现一个索引属性

简化到最低限度:

Option Explicit
Private values As Scripting.Dictionary

Private Sub Class_Initialize()
Set values = New Scripting.Dictionary
End Sub

Public Property Get Something(ByVal key As String) As Double
Something = values(key)
End Property

Public Property Let Something(ByVal key As String, ByVal value As Double)
values(key) = value
End Property

您将字典安全地封装为类的实现细节(例如,外部代码无法将它们设置为 Nothing ),并公开索引的 Get + Let每个封装字典的属性,以索引(/key)作为参数。

就您的 WeatherData 而言类,这意味着您可以像这样填充数据:

Set data = New WeatherData
With data
.Temp("day 1") = 76
.Temp("day 2") = 78
.Humid("day 1") = 0.55
.Humid("day 2") = 0.61
.Wind("day 1") = 0.92
.Wind("day 2") = 1.27
End With

然后检索"day 1"的温度与 data.Temp("day 1") .

至于您的初始化方法,需要从类的实例调用 - 作为实例方法

所以代替 Initialize tester你应该这样做tester.Initialize .

是否将内部封装存储做成数组,CollectionDictionary对调用代码没有区别 - 它是一个封装的实现细节:如果需要,您的类也可以将数据存储在 .csv 文件中或数据库中。

关于vba - 将字典放入类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43262391/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com