2011-05-29 90 views
0

我遇到了另一個SO問題中發佈的這個vba代碼位。在引用類的Get屬性中的Set屬性時是否有任何意義?從Get屬性引用Set屬性

Private WithEvents mctlEventButton As MSForms.CommandButton

Public Property Set EventButton(ctlButton As MSForms.CommandButton)
Set mctlEventButton = ctlButton
End Property

爲什麼這樣做? ...

Public Property Get EventButton() As MSForms.CommandButton
Set EventButton = mctlEventButton
End Property

代碼是示出如何使用collections通過一組控件進行迭代。問題並不是關於代碼的那部分,所以在這個問題中沒有提到。使用問題中的示例,我遇到了一個問題,因爲Get Property正在使用Set屬性。那麼什麼時候會有用?

這裏是鏈接到SO問題:object array or collection in VBA Excel

回答

1
Public Property Get EventButton() As MSForms.CommandButton 
    Set EventButton = mctlEventButton 
End Property 

在上面的代碼,Set EventButton = mctlEventButton不會調用以下(嘗試通過代碼&踩着你會看到,它不步入Set

Public Property Set EventButton(ctlButton As MSForms.CommandButton) 
    Set mctlEventButton = ctlButton 
End Property 

在另一方面,這個它是用來返回一個值的語句。
實際上,認爲Get作爲

Public function EventButton() As MSForms.CommandButton 
    Set EventButton = mctlEventButton 'returning the value from the function 
End Property 

包裹在開發人員的財產形式做得到/套。

+0

我有一個完整的響應排列了如何當我進入代碼,它** **進入'set'語句。然後我意識到我的'Get'和'Set'屬性命名不同。使它們成爲相同的名稱會產生你描述的行爲。謝謝你的解釋。 :) – 2011-05-29 18:24:49