共用方式為


Properties and Property Procedures 

You can use both properties and fields to store information in an object. Whereas fields are simply public variables, properties use property procedures to control how values are set or returned. Property procedures are blocks of code declared within property definitions that you can use to execute code when a property value is set or retrieved.

Visual Basic has two types of property procedures: the Get property procedures for retrieving a property value, and the Set property procedures for assigning a value to a property. For example, a property that stores the balance of a bank account might use code in a Get property procedure to post interest and check for service fees before returning the available balance. You could then use the Set property procedure to validate the balance and prevent it from being incorrectly updated. In short, property procedures allow an object to protect and validate its own data.

The following code fragment compares how fields and properties validate values.

Protected Sub TestFieldsAndProperties()
    ' Assume, for this example, that the only valid values for
    ' the field and property are numbers less than 10.
    Dim NewClass As New ThisClass

    ' Test data validation. 

    ' Works because there is no data validation.
    NewClass.ThisField = 36
    ' Will print 36.
    MsgBox("ThisField = " & NewClass.ThisField)

    ' The attempt to set the field to a value greater than 10 will silently fail.
    NewClass.ThisProperty = 36
    ' The next statement will print the old value of 0 instead.
    MsgBox("ThisProperty = " & NewClass.ThisProperty)
End Sub

Public Class ThisClass
    ' Declare a field.
    Public ThisField As Integer
    ' Field used for Property Set operations.
    Private thisPropertyValue As Integer = 0
    ' Declare a property.
    Public Property ThisProperty() As Integer
        Get
            Return thisPropertyValue
        End Get
        Set(ByVal Value As Integer)
            ' Only allow Set operation for values less than 10.
            If Value < 10 Then thisPropertyValue = Value
        End Set
    End Property
End Class

The procedure TestFieldsAndProperties creates an instance of the class and sets and retrieves the values of the fields and properties. For the sake of this example, it is assumed that the only valid values are numbers less than 10. Because there is no way to validate values assigned to fields, setting the field to value 36 is allowed. The property, however, only performs assignment for numbers less than 10, so the attempt to set it to 36 is ignored.

Read-Only and Write-Only Properties

Most properties have both Get and Set property procedures you can use to read and modify the value stored inside. However, you can use the ReadOnly or WriteOnly modifiers to restrict properties from being modified or read.

Read-only properties cannot have Set property procedures; such properties are useful for items that you want to expose, but not allow to be modified. For example, you could use a read-only property to provide the processor speed of a computer.

Write-only properties cannot have Get property procedures and are useful for configuring objects with data that should not or cannot be stored in the object. For example, a write-only property could be used to take a password and change the object's state without storing the password.

Note

Previous versions of Visual Basic support Let property procedures for use when assigning objects to properties. Visual Basic eliminates the need for Let property procedures because object assignment is handled like any other kind of assignment.

See Also

Reference

Get Statement
Set Statement (Visual Basic)
ReadOnly (Visual Basic)
WriteOnly

Concepts

Property Procedures
Property Procedures vs. Fields