How to: Create a Property (Visual Basic)
You enclose a property definition between a Property
statement and an End Property
statement. Within this definition you define a Get
procedure, a Set
procedure, or both. All the property's code lies within these procedures.
The Get
procedure retrieves the property's value, and the Set
procedure stores a value. If you want the property to have read/write access, you must define both procedures. For a read-only property, you define only Get
, and for a write-only property, you define only Set
.
To create a property
Outside any property or procedure, use a Property Statement, followed by an
End Property
statement.If the property takes parameters, follow the
Property
keyword with the name of the procedure, then the parameter list in parentheses.Follow the parentheses with an
As
clause to specify the data type of the property's value. You must specify the data type even for a write-only property.Add
Get
andSet
procedures, as appropriate. See the following directions.
To create a Get procedure that retrieves a property value
Between the
Property
andEnd Property
statements, write a Get Statement, followed by anEnd Get
statement. You do not need to define any parameters for theGet
procedure.Place the code statements to retrieve the property's value between the
Get
andEnd Get
statements. This code can include other calculations and data manipulations in addition to generating and returning the property's value.Use a
Return
statement to return the property's value to the calling code.
You must write a Get
procedure for a read-write property and for a read-only property. You must not define a Get
procedure for a write-only property.
To create a Set procedure that writes a property's value
Between the
Property
andEnd Property
statements, write a Set Statement, followed by anEnd Set
statement.In the
Set
statement, optionally follow theSet
keyword with a parameter list in parentheses. If the parameter list is not present or is empty, an implicit parameter namedValue
is defined, whose type is the type of the property itself. If the parameter list is not empty, you can use a different name if appropriate, but the parameter must have the same data type as the property itself.Place the code statements to store a value in the property between the
Set
andEnd Set
statements. This code can include other calculations and data manipulations in addition to validating and storing the property's value.Use the value parameter to accept the value supplied by the calling code. You can either store this value directly in an assignment statement, or use it in an expression to calculate the internal value to be stored.
You must write a Set
procedure for a read-write property and for a write-only property. You must not define a Set
procedure for a read-only property.
Example
The following example creates a read/write property that stores a full name as two constituent names, the first name and the last name. When the calling code reads fullName
, the Get
procedure combines the two constituent names and returns the full name. When the calling code assigns a new full name, the Set
procedure attempts to break it into two constituent names. If it does not find a space, it stores it all as the first name.
Dim firstName, lastName As String
Property fullName() As String
Get
If lastName = "" Then
Return firstName
Else
Return firstName & " " & lastName
End If
End Get
Set(ByVal Value As String)
Dim space As Integer = Value.IndexOf(" ")
If space < 0 Then
firstName = Value
lastName = ""
Else
firstName = Value.Substring(0, space)
lastName = Value.Substring(space + 1)
End If
End Set
End Property
The following example shows typical calls to the property procedures of fullName
. The first call sets the property value and the second call retrieves it.
fullName = "MyFirstName MyLastName"
MsgBox(fullName)
See also
- Procedures
- Property Procedures
- Procedure Parameters and Arguments
- Differences Between Properties and Variables in Visual Basic
- How to: Declare a Property with Mixed Access Levels
- How to: Call a Property Procedure
- How to: Declare and Call a Default Property in Visual Basic
- How to: Put a Value in a Property
- How to: Get a Value from a Property
משוב
https://aka.ms/ContentUserFeedback.
בקרוב: במהלך 2024, נפתור בעיות GitHub כמנגנון המשוב לתוכן ונחליף אותו במערכת משוב חדשה. לקבלת מידע נוסף, ראה:שלח והצג משוב עבור