呼叫屬性程式

下表列出呼叫 屬性程式的語法:

屬性程序 語法
Property Get [Set ] varname = [ object.] propname [ ( [arguments] ) ]
Property Let [Let ][ object.] propname [ ( [arguments] ) ] = argument
屬性集 設定 [ object.] propname [ ( [arguments] ) ] = objectArg

屬性過程調用需要至少一個自變數、指 派 (=) 運算元,以及屬性過程名稱。

  • 在指派運算 右側具有屬性名稱的呼叫中,Visual Basic 會呼叫 Property Get 以從類別/物件傳回資訊。
  • 在指派運算 左側具有屬性名稱的呼叫中,Visual Basic 會呼叫 Property LetProperty Set 來更新類別物件內的資訊。

如果屬性過程的宣告有多個參數,呼叫 Property LetProperty Set,則會將指派運算符右側的 自變數傳遞至 Property LetProperty Set 程式的最後一個參數

例如,下圖使用 Property Let 來顯示屬性過程調用 (頂端) 的自變數如何與下) 上宣告 (中的參數相關:

Property Let

下列程式代碼範例示範屬性過程自變數和參數之間的關聯性。

'DemoType class declaration
Private pW
Private pX
Private pY
Private pZ

Property Get DemoProperty(w, x, y)
    'Calling format is: `z = DemoProperty(w, x, y)`
    ' or `Set z = DemoProperty(w, x, y)`
    w = pW
    x = pX
    y = pY
    If IsObject(pZ) Then
        Set DemoProperty = pZ
    Else
        DemoProperty = pZ
    End If
End Property
Property Let DemoProperty(w, x, y, z)
    'Calling format is `DemoProperty(w, x, y) = z`
    pW = w
    pX = x
    pY = y
    pZ = z
End Property
Property Set DemoProperty(w, x, y, z As Object)
    'Calling format is `Set DemoProperty(w, x, y) = z`
    pW = w
    pX = x
    pY = y
    Set pZ = z
End Property
Sub DemoSub()
    Dim myDemo As Object
    Dim a, b, c, d
    Dim w, x, y, z

    Set myDemo = New DemoType

    a = "Hello"
    b = ", "
    c = "world"
    d = "!"

    Debug.Print Join(Array(a, b, c, d), "") ' Hello, world!a

    'Call Property Let DemoProperty(a, b, c, d)
    Let myDemo.DemoProperty(a, b, c) = d
    'Call Property Get
    d = myDemo.DemoProperty(a, b, c)

    Debug.Print Join(Array(a, b, c, d), "") ' Hello, world!
End Sub

在實務上,具有多個自變數之屬性程式的唯一用法是建立屬性陣列

另請參閱

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應