Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Declares a Get property procedure used to retrieve the value of a property.
Syntax
[ <attributelist> ] [ accessmodifier ] Get()
[ statements ]
End Get
Parts
| Term | Definition |
|---|---|
attributelist |
Optional. See Attribute List. |
accessmodifier |
Optional on at most one of the Get and Set statements in this property. Can be one of the following:- Protected - Friend - Private - Protected FriendSee Access levels in Visual Basic. |
statements |
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. |
Remarks
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 the Property Statement and 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 Private 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.
Rules
Mixed Access Levels. If you are defining a read-write property, you can optionally specify a different access level for either the
Getor theSetprocedure, 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 declaredFriend, you can declare theGetprocedurePrivate, but notPublic.If you are defining a
ReadOnlyproperty, theGetprocedure represents the entire property. You cannot declare a different access level forGet, 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. The
Getprocedure automatically returns that data type. You can specify any data type or the name of an enumeration, structure, class, or interface.If the
Propertystatement does not specifyreturntype, the procedure returnsObject.
Behavior
Returning from a Procedure. When the
Getprocedure returns to the calling code, execution continues within the statement that requested the property value.Getproperty procedures can return a value using either the Return Statement or by assigning the return value to the property name. For more information, see "Return Value" in Function Statement.The
Exit PropertyandReturnstatements cause an immediate exit from a property procedure. Any number ofExit PropertyandReturnstatements can appear anywhere in the procedure, and you can mixExit PropertyandReturnstatements.Return Value. To return a value from a
Getprocedure, you can either assign the value to the property name or include it in a Return Statement. TheReturnstatement simultaneously assigns theGetprocedure return value and exits the procedure.If you use
Exit Propertywithout assigning a value to the property name, theGetprocedure returns the default value for the property's data type. For more information, see "Return Value" in Function Statement.The following example illustrates two ways the read-only property
quoteForTheDaycan return the value held in the private variablequoteValue.Private quoteValue As String = "No quote assigned yet."ReadOnly Property QuoteForTheDay() As String Get QuoteForTheDay = quoteValue Exit Property End Get End PropertyReadOnly Property QuoteForTheDay() As String Get Return quoteValue End Get End Property
Example
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