Visual Basic Language Reference  

Property Statement

Declares the name of a property, and the property procedures used to store and retrieve the value of the property.

[ <attrlist> ] [ Default ] [ Public | Private | Protected | Friend | Protected Friend ] _
[[ ReadOnly | WriteOnly ] [ Overloads | Overrides ] _
[ Overridable | NotOverridable ] | MustOverride | Shadows | Shared ] _
Property varname([ ByVal parameter list ]) [ As typename ] [ Implements interfacemember ]
   [ <attrlist> ] Get
      [ block ]
   End Get
   [ <attrlist> ] Set(ByVal value As typename )
      [ block ]
   End Set
End Property

Parts

attrlist
Optional. List of attributes that apply to this property. Multiple attributes are separated by commas.
Default
Optional. Declares a default property. Default properties must accept parameters and can be set and retrieved without specifying the property name.
Overloads
Optional. Indicates that this property overloads one or more properties defined with the same name in a base class. The argument list in this declaration must be different from the argument list of every overloaded property. The lists must differ in the number of arguments, their data types, or both. This allows the compiler to distinguish which version to use.

You do not have to use the Overloads keyword when you are defining multiple overloaded properties in the same class. However, if you use Overloads in one of the declarations, you must use it in all of them.

You cannot specify both Overloads and Shadows in the same property declaration.

Overrides
Optional. Indicates that this property overrides an identically named property in a base class. The number and data types of the arguments, and the data type of the return value, must exactly match those of the base class property.
Overridable
Optional. Indicates that this property can be overridden by an identically named property in a derived class.
NotOverridable
Optional. Indicates that this property cannot be overridden in a derived class. Properties are NotOverridable by default but you cannot specify the NotOverridable modifier with properties or methods that do not override another member.
MustOverride
Optional. Indicates that this property is not implemented in this class, and must be implemented in a derived class for that class to be creatable.
Shadows
Optional. Indicates that this property shadows an identically named programming element, or set of overloaded elements, in a base class. You can shadow any kind of declared element with any other kind. If you shadow a property with another property, the arguments and the return type do not have to match those in the base class property. A shadowed element is unavailable in the derived class that shadows it.
Note   You cannot specify both Overloads and Shadows in the same property declaration.
Shared
Optional. Indicates that this is a shared property. This means it is not associated with a specific instance of a class or structure. You can call a shared property by qualifying it either with the class or structure name, or with the variable name of a specific instance of the class or structure.
Public
Optional. Entities declared with the Public modifier have public access. There are no restrictions on the use of public entities. Properties that do not specify an access modifier are Public by default.
Private
Optional. Entities declared with the Private modifier have private access. A private entity is accessible only within its declaration context, including any nested entities.
Protected
Optional. Entities declared with the Protected keyword have protected access. They are accessible only from within their own class or from a derived class. Protected access can be specified only on members of classes. It is not a superset of friend access.
Friend
Optional. Entities declared with the Friend modifier have friend access. An entity with friend access is accessible only within the program that contains the entity declaration.
Protected Friend
Optional. Entities declared with the Protected Friend modifiers have the union of protected and friend accessibility.
ReadOnly
Optional. Indicates that a properties value can be retrieved, but it cannot be the modified. ReadOnly properties contain Get blocks but lack Set blocks.
WriteOnly
Optional. Indicates that a property can be the target of assignment but its value cannot be retrieved. WriteOnly properties contain Set blocks but lack Get blocks.
varname
Required. A unique name that identifies the property.
parameter list
Optional. This identifies the signature of the property. Parameters for properties must be passed ByVal.
typename
Optional. If no data type is specified, the default type is Object. If the property is implementing a property in an interface, typename must match the type declared in the interface.
Implements
Optional. Indicates that this property implements a property of an interface.
interfacemember
Optional. When a property is part of a class that implements an interface, this is the name of the property being implemented.
Get
Starts a Get property procedure used to return the value of a property. Get blocks are required unless the property is marked WriteOnly.
End Get
Terminates a Get property procedure.
Set
Starts a Set property procedure used to set the value of a property. Set blocks are required unless the property is marked ReadOnly. The new value of a property is passed to the Set property procedure in a parameter named value when the value of the property changes
End Set
Terminates a Set property procedure.
End Property
Terminates a Property definition.

Each attribute in the attrlist part has the following syntax and parts:

attrname [({ attrargs | attrinit })]

attrlist Parts

attrname
Required. Name of the attribute. Must be a valid Visual Basic identifier.
attrargs
Optional. List of positional arguments for this attribute. Multiple arguments are separated by commas.
attrinit
Optional. List of field or property initializers for this attribute. Multiple initializers are separated by commas.

Remarks

Visual Basic .NET passes a parameter to the Set block during property assignments. If you do not supply a parameter for Set, the integrated development environment (IDE) supplies a parameter named Value. The parameter contains the contents of the item that was assigned to the property when the Set block was called. The contents of the parameter are usually stored in a private local variable and returned whenever the Get block is called.

The declaration of the property determines what the user can do with that 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

See Also

Adding Fields and Properties to a Class | Get Statement | Set Statement | Default Properties