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.
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
To create a property
Outside any property or procedure, use a Property Statement, followed by an
If the property takes parameters, follow the
Propertykeyword with the name of the procedure, then the parameter list in parentheses.
Follow the parentheses with an
Asclause to specify the data type of the property's value. You must specify the data type even for a write-only property.
Setprocedures, as appropriate. See the following directions.
To create a Get procedure that retrieves a property value
End Propertystatements, write a Get Statement, followed by an
End Getstatement. You do not need to define any parameters for the
Place the code statements to retrieve the property's value between the
End Getstatements. This code can include other calculations and data manipulations in addition to generating and returning the property's value.
Returnstatement 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
End Propertystatements, write a Set Statement, followed by an
Setstatement, optionally follow the
Setkeyword with a parameter list in parentheses. If the parameter list is not present or is empty, an implicit parameter named
Valueis 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
End Setstatements. 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.
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
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)
- 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