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, followed by an End Set statement.
In the Set statement, optionally follow the Set keyword with a parameter list in parentheses. If the parameter list is not present or is empty, an implicit parameter named Value 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 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.
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.
VB
Dim firstName, lastName AsStringProperty fullName() AsStringGetIf lastName = ""ThenReturn firstName
ElseReturn firstName & " " & lastName
EndIfEndGetSet(ByVal Value AsString)
Dim space AsInteger = Value.IndexOf(" ")
If space < 0Then
firstName = Value
lastName = ""Else
firstName = Value.Substring(0, space)
lastName = Value.Substring(space + 1)
EndIfEndSetEndProperty
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.
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Learn how to implement read-write, read-only, and write-only class properties using property accessors and access modifiers, and how to implement methods and extension methods for a class.