Declares a Get property procedure used to retrieve the value of a property.
Optional. See Attribute List.
Optional on at most one of the Get and Set statements in this property. Can be one of the following:
Optional. One or more statements that run when the Get property procedure is called.
- End Get
Required. Terminates the definition of the Get property procedure.
Every property must have a Get property procedure unless the property is marked WriteOnly. The Get procedure is used to return the current value of the property.
Visual Basic automatically calls a property's Get procedure when an expression requests the property's value.
The body of the property declaration can contain only the property's Get and Set procedures between theand the End Property statement. It cannot store anything other than those procedures. In particular, it cannot store the property's current value. You must store this value outside the property, because if you store it inside either of the property procedures, the other property procedure cannot access it. The usual approach is to store the value in a variable declared at the same level as the property. You must define a Get procedure inside the property to which it applies.
The Get procedure defaults to the access level of its containing property unless you use accessmodifier in the Get statement.
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 Get procedure Private, but not Public.
If you are defining a ReadOnly property, the Get procedure represents the entire property. You cannot declare a different access level for Get, because that would set two access levels for the property.
Return Type. Thecan declare the data type of the value it returns. The Get procedure automatically returns that data type. You can specify any data type or the name of an enumeration, structure, class, or interface.
If the Property statement does not specify returntype, the procedure returns Object.
Returning from a Procedure. When the Get procedure returns to the calling code, execution continues within the statement that requested the property value.
Get property procedures can return a value using either theor by assigning the return value to the property name. For more information, see "Return Value" in .
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. The Return statement simultaneously assigns the Get procedure return value and exits the procedure.
If you use Exit Property without assigning a value to the property name, the Get procedure returns the default value for the property's data type. For more information, see "Return Value" in.
The following example illustrates two ways the read-only property quoteForTheDay can return the value held in the private variable quoteValue.
ReadOnly Property quoteForTheDay() As String Get quoteForTheDay = quoteValue Exit Property End Get End Property
The following example uses the Get statement to return the value of a property.
Class propClass ' Define a private local variable to store the property value. Private currentTime As String ' Define the read-only property. Public ReadOnly Property dateAndTime() As String Get ' The Get procedure is called automatically when the ' value of the property is retrieved. currentTime = CStr(Now) ' Return the date and time As a string. Return currentTime End Get End Property End Class