How to: Create a Property
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 and Set procedures, as appropriate. See the following directions.
To create a Get procedure that retrieves a property value
Between the Property and End Property statements, write a Get Statement, followed by an End Get statement. You do not need to define any parameters for the Get procedure.
Place the code statements to retrieve the property's value between the Get and End 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 and End Property statements, write a Set Statement (Visual Basic), followed by an End Set statement.
In the Set statement, follow the Set keyword with a parameter list in parentheses. This parameter list must include at least a value parameter for the value passed by the calling code. The default name for this value parameter is Value, but you can use a different name if appropriate. The value 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 and End 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.
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.