Get property procedure used to retrieve the value of a property.
[ <attributelist> ] [ accessmodifier ] Get() [ statements ] End Get
||Optional. See Attribute List.|
||Optional on at most one of the
See Access levels in Visual Basic.
||Optional. One or more statements that run when the
||Required. Terminates the definition of the
Every property must have a
Get property procedure unless the property is marked
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
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.
Get procedure defaults to the access level of its containing property unless you use
accessmodifier in the
Mixed Access Levels. If you are defining a read-write property, you can optionally specify a different access level for either the
Setprocedure, 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
Private, but not
If you are defining a
Getprocedure 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. 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.
Propertystatement does not specify
returntype, the procedure returns
Returning from a Procedure. When the
Getprocedure returns to the calling code, execution continues within the statement that requested the property value.
Returnstatements cause an immediate exit from a property procedure. Any number of
Returnstatements can appear anywhere in the procedure, and you can mix
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. The
Returnstatement simultaneously assigns the
Getprocedure return value and exits the procedure.
If you use
Exit Propertywithout assigning a value to the property name, the
Getprocedure 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 variable
Private quoteValue As String = "No quote assigned yet."
ReadOnly Property quoteForTheDay() As String Get quoteForTheDay = quoteValue Exit Property End Get End Property
ReadOnly Property quoteForTheDay() As String Get Return quoteValue 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
We'd love to hear your thoughts. Choose the type you'd like to provide:
Our feedback system is built on GitHub Issues. Read more on our blog.