Gra*_*rry 5 excel vba excel-vba
几周前,我得到了关于如何在此位置制作通用类模块的出色答案:类“ let”陷入无限循环
老实说,我仍然不太了解,因为我的vba知识100%是自学的,从这里开始,几年前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
Run Code Online (Sandbox Code Playgroud)
然后,我尝试使用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
Run Code Online (Sandbox Code Playgroud)
并输入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
Run Code Online (Sandbox Code Playgroud)
tl; dr:制作数组而不是字典,并完全删除初始化。您的“键”别无选择,只能是数字,但至少有效。如果有人愿意,我真的很想知道一个实际的解决方案,但是我遇到的具体问题已经解决了。
似乎要实现索引属性。
简化到最低限度:
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
Run Code Online (Sandbox Code Playgroud)
您将字典安全地封装为类的实现细节(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
Run Code Online (Sandbox Code Playgroud)
然后检索的温度"day 1"用data.Temp("day 1")。
至于初始化方法,则需要从类的实例(即实例方法)中调用它。
所以不是Initialize tester你应该做的tester.Initialize。
无论您将内部封装的存储区设置为数组,Collection还是将a 或a Dictionary与调用代码都没有区别-这是封装的实现细节:如果需要,您的类也可以将数据存储在.csv文件或数据库中。