【发布时间】:2017-09-01 21:50:02
【问题描述】:
几周前,我得到了一个关于如何在这个位置制作通用类模块的精彩答案:Class "let" stuck in infinite loop
老实说,我仍然不太了解,因为我 100% 的 vba 知识都是自学的,并且从这里开始,几年前的一个学期 C 中剩下的一些通用编程逻辑。但我认为我对此有很好的把握,因为这是一个很好的解释。我现在正在尝试将其应用于我的课程中的字典并遇到一些麻烦。
我的班级模块如下:
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对此进行测试。那没有用。然后我将 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
并输入Initialize tester,但这也不起作用(“编译错误:未定义子或函数”)。
那么,结束问题:如何将三个字典放在一个类模块中?
编辑:我是个傻瓜。以下内容本身并没有真正解决问题,但它至少绕过了它,以至于我不必承认它:
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:制作数组而不是字典,并完全删除初始化。你的“钥匙”别无选择,只能是数字,但至少它有效。如果有人愿意,我真的很想知道一个实际的解决方案,但我遇到的具体问题已经解决了。
【问题讨论】:
-
“它不起作用”并不是一个明确的问题陈述,但
Temp、Humid和Wind都暴露为Double's - 你不能分配他们到Dictionary参考... -
我这样做是因为它将成为一本双打字典,尽管我不知道它是否能让我有所收获。那么这会作为一个数组工作吗?我编辑添加返回的错误是关于
Initialize的编译错误。 -
Initialize方法是WeatherData的成员? -
Initialize 是一个函数,可以将 24 个条目添加到
WeatherData中的每个字典(/array/whatever 最终工作)。从那里,随着条目实际存在,它们可以更改为读取的任何数据。