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 the name of a property, and the property procedures used to store and retrieve the value of the property.
Syntax
[ <attributelist> ] [ Default ] [ accessmodifier ]
[ propertymodifiers ] [ Shared ] [ Shadows ] [ ReadOnly | WriteOnly ] [ Iterator ]
Property name ( [ parameterlist ] ) [ As returntype ] [ Implements implementslist ]
[ <attributelist> ] [ accessmodifier ] Get
[ statements ]
End Get
[ <attributelist> ] [ accessmodifier ] Set ( ByVal value As returntype [, parameterlist ] )
[ statements ]
End Set
End Property
- or -
[ <attributelist> ] [ Default ] [ accessmodifier ]
[ propertymodifiers ] [ Shared ] [ Shadows ] [ ReadOnly | WriteOnly ]
Property name ( [ parameterlist ] ) [ As returntype ] [ Implements implementslist ]
Parts
attributelistOptional. List of attributes that apply to this property or
GetorSetprocedure. See Attribute List.DefaultOptional. Specifies that this property is the default property for the class or structure on which it is defined. Default properties must accept parameters and can be set and retrieved without specifying the property name. If you declare the property as
Default, you cannot usePrivateon the property or on either of its property procedures. For examples and detailed guidance, see How to: Declare and Call a Default Property in Visual Basic.accessmodifierOptional on the
Propertystatement and on at most one of theGetandSetstatements. Can be one of the following:propertymodifiersOptional. Can be one of the following:
MustOverride OverridesNotOverridable Overrides
SharedOptional. See Shared.
ShadowsOptional. See Shadows.
ReadOnlyOptional. See ReadOnly.
WriteOnlyOptional. See WriteOnly.
IteratorOptional. See Iterator.
nameRequired. Name of the property. See Declared Element Names.
parameterlistOptional. List of local variable names representing the parameters of this property, and possible additional parameters of the
Setprocedure. Parameterized properties are often used to create indexers or default properties that allow collection-like access. See Parameter List and How to: Declare and Call a Default Property in Visual Basic.returntypeRequired if
Option StrictisOn. Data type of the value returned by this property.ImplementsOptional. Indicates that this property implements one or more properties, each one defined in an interface implemented by this property's containing class or structure. See Implements Statement.
implementslistRequired if
Implementsis supplied. List of properties being implemented.implementedproperty [ , implementedproperty ... ]Each
implementedpropertyhas the following syntax and parts:interface.definednamePart Description interfaceRequired. Name of an interface implemented by this property's containing class or structure. definednameRequired. Name by which the property is defined in interface.GetOptional. Required if the property is marked
ReadOnly. Starts aGetproperty procedure that is used to return the value of the property. TheGetstatement is not used with automatically implemented properties.statementsOptional. Block of statements to run within the
GetorSetprocedure.End GetTerminates the
Getproperty procedure.SetOptional. Required if the property is marked
WriteOnly. Starts aSetproperty procedure that is used to store the value of the property. TheSetstatement is not used with automatically implemented properties.End SetTerminates the
Setproperty procedure.End PropertyTerminates the definition of this property.
Remarks
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 omit the Get and Set procedure when using an automatically implemented property. For more information, see Automatically implemented properties.
You can use Property only at class 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.
By default, properties use 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. For detailed examples of mixed access levels, see How to: Declare a Property with Mixed Access Levels.
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
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 theSetprocedurePrivate, but notPublic.If you are defining a
ReadOnlyorWriteOnlyproperty, the single property procedure (GetorSet, respectively) represents all of the 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
Propertystatement 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 returnsObject.Implementation. If this property uses the
Implementskeyword, the containing class or structure must have anImplementsstatement immediately following itsClassorStructurestatement. TheImplementsstatement must include each interface specified inimplementslist. However, the name by which an interface defines theProperty(indefinedname) does not have to be the same as the name of this property (inname).
Behavior
Returning from a Property Procedure. When the
GetorSetprocedure returns to the calling code, execution continues with the statement following the statement that invoked it.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 aReturnstatement. The following example assigns the return value to the property namequoteForTheDayand then uses theExit Propertystatement to return.Private quoteValue As String = "No quote assigned yet."ReadOnly Property QuoteForTheDay() As String Get QuoteForTheDay = quoteValue Exit Property End Get End PropertyIf you use
Exit Propertywithout assigning a value toname, theGetprocedure returns the default value for the property's data type.The
Returnstatement at the same time assigns theGetprocedure 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
Example
The following example declares a property in a class.
Class Class1
' Define a local variable to store the property value.
Private propertyValue As String
' Define the property.
Public Property Prop1() As String
Get
' The Get property procedure is called when the value
' of a property is retrieved.
Return propertyValue
End Get
Set(ByVal value As String)
' The Set property procedure is called when the value
' of a property is modified. The value to be assigned
' is passed in the argument to Set.
propertyValue = value
End Set
End Property
End Class
Parameterized Properties
The following example shows how to create a parameterized property, also called an indexer, which allows array-like access to a collection:
Class SampleCollection
' Define a local collection to store strings.
Private items As New List(Of String)
' Define a parameterized property (indexer) for the collection.
Default Public Property Item(ByVal index As Integer) As String
Get
' Return the item at the specified index.
If index >= 0 AndAlso index < items.Count Then
Return items(index)
Else
Return Nothing
End If
End Get
Set(ByVal value As String)
' Set the item at the specified index.
If index >= 0 AndAlso index < items.Count Then
items(index) = value
ElseIf index = items.Count Then
' Allow adding new items at the end.
items.Add(value)
End If
End Set
End Property
' Add a Count property for convenience.
Public ReadOnly Property Count As Integer
Get
Return items.Count
End Get
End Property
' Add method to add items.
Public Sub Add(ByVal item As String)
items.Add(item)
End Sub
End Class
For comprehensive examples of property usage, including automatic implementation, mixed access levels, and validation scenarios, see Property Procedures.
See also
Property Types and Features
Advanced Property Scenarios
- How to: Declare and Call a Default Property in Visual Basic
- How to: Declare a Property with Mixed Access Levels