2011-02-18 312 views
30

我剛剛創建了幾個Property Set方法,它們沒有編譯。當我將它們更改爲Property Let時,一切都很好。在VB6中Property Set和Property Let有什麼區別?

我已經研究過文檔,找到Property SetProperty Let之間的區別,但必須承認沒有更明智的做法。有沒有什麼區別,如果有的話可以提供一個指針,以正確解釋它?

回答

22

Property Set爲對象(例如,類實例)

Property Let爲 「正常」 的數據類型(例如,字符串,布爾值,長整型等)

3

Property Set爲而Property Let類似對象的變量(爲ByRef)爲值狀變量(BYVAL)

+2

它不是ByRef與ByVal問題,但嚴格用於返回對調用者的對象引用。 – Bob77 2011-02-18 15:30:30

+0

的確,這不是ByRef/ByVal問題。但它不是用於返回調用者的對象引用。爲此,您使用`Property Get`與您返回任何其他類屬性(沒有用於返回類屬性的特定於對象的版本)相同。 – mwolfe02 2011-02-18 16:20:41

+0

是的,我剛剛倒過來,是爲了分配一個對象屬性 - 呃,那裏真的很糟糕。 – Bob77 2011-02-18 17:49:51

22

Property LetProperty Set更通用。後者僅限於對象引用。如果你在一個類中有此屬性

Private m_oPicture   As StdPicture 

Property Get Picture() As StdPicture 
    Set Picture = m_oPicture 
End Property 

Property Set Picture(oValue As StdPicture) 
    Set m_oPicture = oValue 
End Property 

Property Let Picture(oValue As StdPicture) 
    Set m_oPicture = oValue 
End Property 

您可以致電Property Set Picture

Set oObj.Picture = Me.Picture 

您可以致電Property Let Picture

Let oObj.Picture = Me.Picture 
oObj.Picture = Me.Picture 

實施Property Set是什麼其他的開發人員期望的性質是對象引用,但有時甚至Microsoft僅提供參考屬性Property Let,導致unu sual語法oObj.Object = MyObject沒有Set聲明。在這種情況下,使用Set語句會導致編譯時或運行時錯誤,因爲在oObj類上沒有執行Property Set Object

我傾向於實現Property SetProperty Let標準類型的屬性 - 字體,圖片等 - 但具有不同的語義。通常在Property Let我傾向於執行「深層複製」,即克隆StdFont而不是僅僅保存對原始對象的引用。

相關問題