The Property statement introduces the declaration of a property. A property can have a Get procedure (read only), a Set procedure (write only), or both (read-write).
You can use Property only at module level. This means the declaration context for a property must be a class, structure, module, or interface, and cannot be a source file, namespace, procedure, or block. For more information, see Declaration Contexts and Default Access Levels.
Properties default to public access. You can adjust a property's access level with an access modifier on the Property statement, and you can optionally adjust one of its property procedures to a more restrictive access level.
Visual Basic passes a parameter to the Set procedure during property assignments. If you do not supply a parameter for Set, the integrated development environment (IDE) uses an implicit parameter named value. This parameter holds the value to be assigned to the property. You typically store this value in a private local variable and return it whenever the Get procedure is called.
Rules
-
Mixed Access Levels. If you are defining a read-write property, you can optionally specify a different access level for either the Get or the Set procedure, but not both. If you do this, the procedure access level must be more restrictive than the property's access level. For example, if the property is declared Friend, you can declare the Set procedure Private, but not Public.
If you are defining a ReadOnly or WriteOnly property, the single property procedure (Get or Set, respectively) represents the entire property. You cannot declare a different access level for such a procedure, because that would set two access levels for the property.
-
Return Type. The Property statement can declare the data type of the value it returns. You can specify any data type or the name of an enumeration, structure, class, or interface.
If you do not specify returntype, the property returns Object.
-
Implementation. If this property uses the Implements keyword, the containing class or structure must have an Implements statement immediately following its Class or Structure statement. The Implements statement must include each interface specified in implementslist. However, the name by which an interface defines the Property (in definedname) does not have to be the same as the name of this property (in name).
Behavior
-
Returning from a Property Procedure. When the Get or Set procedure returns to the calling code, execution continues with the statement following the statement that invoked it.
The Exit Property and Return statements cause an immediate exit from a property procedure. Any number of Exit Property and Return statements can appear anywhere in the procedure, and you can mix Exit Property and Return statements.
-
Return Value. To return a value from a Get procedure, you can either assign the value to the property name or include it in a Return statement. The following example assigns the return value to the property name quoteForTheDay and then uses the Exit Property statement to return.
Private quoteValue As String = "No quote assigned yet."
ReadOnly Property quoteForTheDay() As String
Get
quoteForTheDay = quoteValue
Exit Property
End Get
End Property
If you use Exit Property without assigning a value to name, the Get procedure returns the default value for the property's data type.
The Return statement simultaneously assigns the Get procedure return value and exits the procedure. The following example shows this.
Private quoteValue As String = "No quote assigned yet."
ReadOnly Property quoteForTheDay() As String
Get
Return quoteValue
End Get
End Property