2017-04-06 62 views
2

幾個星期前,我就如何在這個位置做一個普通類模塊神話般答案: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;博士:讓數組來代替字典,並完全初始化。你的「鑰匙」別無選擇,只能算數字,但至少可以工作。如果任何人都如此傾向,我會真正有興趣知道一個實際的解決方案,但我解決的具體問題已經解決。

+1

「它不起作用」並不完全是一個明確的問題陳述,但是Temp,Humid和Wind都暴露爲Double,你不能將它們分配給Dictionary '參考... –

+0

我這樣做是因爲它將成爲雙打字典,儘管我不知道它是否會讓我得到任何地方。那麼這會作爲一個數組工作嗎?我編輯補充說,返回的錯誤是關於'Initialize'的編譯錯誤。 –

+0

'Initialize'方法是'WeatherData'的成員? –

回答

1

似乎你想實施一個索引性質

簡化到最低限度:

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文件或到一個數據庫中的數據如果它想要的話。

+0

我有這個工作了一會兒,我興奮起來,但後來停止了。我把你的代碼放在你的代碼中,然後在直接窗口中,我設置了tester = new weatherdata,然後是'debug.print tester.values.count',我得到一個消息,說明該對象不支持該屬性/方法。 –

+0

'values'是'Private',你不能從外部訪問它(它是整個點!) - 如果你的外部代碼需要知道有多少值,那麼你需要暴露'TempItemCount','' HumidItemCount'和'WindItemCount'屬性返回相應內部集合的'.Count'。 –

+0

好吧,所以我想我錯過的是我引用類中的函數,而不是字典對象?我不需要訪問'.count',我只是將它用作測試設備來查看代碼是否工作。但是,不是'debug.print tester.values',而是適當的形式是'tester.something(key)'?如果我想要放入多個字典,我會同時聲明'values'和'values2',然後將'something2'與'something2'完全一樣,除了作用於'values2'? –

0

在這種情況下,你應該按如下方式使用後期綁定:

Private Type categories 
    Temp As Object 
    Humid As Object 
    Wind As Object 
End Type 

Private this As categories 

Public Sub Initialize() 
    Set this.Temp = CreateObject("Scripting.Dictionary") 
    Set this.Humid = CreateObject("Scripting.Dictionary") 
    Set this.Wind = CreateObject("Scripting.Dictionary") 
End Sub 

而且你不能使用多個參數用Let。你應該用一個函數來做到這一點:

Public Function SetTemp(ByVal HourIndex As Long, ByVal Value As Double) 
    this.Temp(HourIndex) = Value 
End Function 

要運行這個我用:

Sub test() 
    Dim multi As Dictionaries 
    Set multi = New Dictionaries 

    multi.Initialize 

    multi.SetTemp 13, 25.522 
    Debug.Print multi.Temp(13) 
End Sub 

凡我類模塊被命名爲Dictionaries。所以基本上使用後期綁定,並將所有多參數讓函數改爲簡單函數。

+0

只要參考值已設定,他就沒有理由遲到。有機會,但參考只是設置不正確。 –