幾個星期前,我就如何在這個位置做一個普通類模塊神話般答案: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'的成員? –