几个星期前,我就如何在这个位置做一个普通类模块神话般答案:Class "let" stuck in infinite loop把词典分成类
我仍然不知道那么多,说实话,因为我的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
然后我试着用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
,进入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;博士:让数组来代替字典,并完全初始化。你的“钥匙”别无选择,只能算数字,但至少可以工作。如果任何人都如此倾向,我会真正有兴趣知道一个实际的解决方案,但我解决的具体问题已经解决。
“它不起作用”并不完全是一个明确的问题陈述,但是Temp,Humid和Wind都暴露为Double,你不能将它们分配给Dictionary '参考... –
我这样做是因为它将成为双打字典,尽管我不知道它是否会让我得到任何地方。那么这会作为一个数组工作吗?我编辑补充说,返回的错误是关于'Initialize'的编译错误。 –
'Initialize'方法是'WeatherData'的成员? –