Property Statement


Property Statement

Syntax

     [Default] [accessModifier] [propModifier] [Shared] [Shadows] _           [ReadOnly | WriteOnly] Property name [(ByVal arglist)] _           [As type] [Implements implementsList]        [subAccessModifier] Get           [statements]           [Exit Property | Return expression]           [statements]        End Get        [subAccessModifier] Set(ByVal value As type)           [statements]           [Exit Property]           [statements]        End Set     End Property 


Default (optional)

Indicates that the property is the default property. Both a Get and a Set block must be defined for default properties. Only one property in a class definition can be the default.


accessModifier (optional)

Specifies the primary scope and accessibility of the property. This setting can be further restricted in the Get or Set blocks, beginning with Visual Basic 2005. One of the following access levels:

Access level

Description

Public

The property is publicly accessible anywhere, both inside and outside of the project.

Private

The property is accessible only within the defining type. The default property of a class cannot be Private.

Protected

The property is accessible only to the code in the defining type or to one of its derived types.

Friend

The property is accessible only within the project that contains the property definition.

Protected Friend

Combines the access features of Protected and Friend.


If omitted, the Public access level is used.


procModifier (optional)

One of the keywords shown in the following table:

Keyword

Description

Overloads

Indicates that more than one declaration of this property exists, each with a different argument signature

Overrides

For derived classes, indicates that the property overrides a property with the same name and argument signature in the base class

Overridable

Indicates that the property can be overridden in a derived class

NotOverridable

Indicates that the property cannot be overridden in a derived class

MustOverride

Indicates that the property must be overridden in a derived class



Shared (optional)

Indicates that the property is shared and not an instance property. Shared properties may be called without a particular instance of the type in which they appear. Shared properties are also known as static properties.


Shadows (optional)

Indicates that the property shadows an identically named element in a base class.


ReadOnly (optional)

Indicates that the property is read-only. The Get block must be supplied; the Set block must be absent.


WriteOnly (optional)

Indicates that the property is write-only. The Set block must be supplied; the Get block must be absent.


name (required)

The name of the property.


arglist (optional; any)

A comma-delimited list of parameters to be supplied to the property as arguments from the calling routine. The value assigned to the property from the calling procedure is not included in this argument list.

arglist uses the following syntax and parts:

     [Optional] ByVal [ParamArray] varname[(  )] _        [As argtype] [= defaultValue] 


Optional (optional)

Flags an argument as optional; optional arguments need not be supplied by the calling routine. All arguments following an optional argument must also be optional. A ParamArray argument cannot be optional.


ByVal (required)

The argument is passed by value; the local copy of the variable is assigned the value of the argument. ByVal is the default method of passing variables. All arguments passed to properties must be passed by value.


ParamArray (optional)

The argument is an optional array containing an arbitrary number of elements. It can only be used as the last element of the argument list and cannot be modified by the Optional keyword. If Option Strict is on, the array type must also be specified.


varname (required)

The name of the argument as used in the local property.


argtype (optional; Type)

The data type of the argument. Any valid .NET data type can be used.


defaultValue (optional; any)

For optional arguments, indicates the default value to be supplied when the calling routine does not supply the value. When the Optional keyword is used, this default value is required.


type (optional; Type)

The return data type of the property. It is also the data type of the value assigned to the property by the calling procedure, as processed by the Set block of the property. The default data type is Object.


implementsList (optional)

Comma-separated list of the interface members implemented by this property.


subAccessModifier (optional)

New in 2005. This sub-access modifier allows one portion of the property to impose a more restrictive access level than is used for the other portion. If both a Get and a Set block are defined, one of them can also include a sub-access modifier. This modifier is one of the following keywords: Private, Protected, Friend, or Protected Friend. It has the same impact as the modifier used for the property's accessModifier, but it must be more restrictive than that modifier.


statements (optional)

Program code to be executed within the property.


expression (optional)

The value to return from the Get block of the property to the calling procedure.


value (required)

The value assigned to the property from the calling procedure. By tradition, this parameter is always named "value."

Description

The Property statement declares a class property, including distinct assignment (Set) and retrieval (Get) accessors. Properties can appear within classes, structures, or modules.

Usage at a Glance

  • Overloads and Shadows cannot be used in the same property declaration.

  • In the Get block, the property value can be returned either by using the Return statement or by assigning the value to a variable with a name that is the same as the property.

  • The Default keyword can be used only with parameterized properties. Typically, these are properties that either return collection items or are implemented as property arrays.

  • A Property Get procedure is used like a function; its return value can be assigned or used just like a function's return value.

         Dim someValue = someClass.SomeProperty 

  • The calling routine assigns a value to a property through a standard assignment statement. The assigned value becomes the value argument in the Set block.

         someClass.SomeProperty = someValue 

  • If an Exit Property or Return statement is used, the property procedure exits and program execution continues with the statement following the call to the property. Any number of Exit Property or Return statements can appear in a property.

  • The value managed by a property is usually a Private variable within the class or type. This adheres to accepted object-oriented techniques by protecting the property value from accidental modification with invalid data.

         Private hiddenSalary As Decimal     Public Property Salary(  ) As Decimal        Get           Return hiddenSalary        End Get        Set(ByVal value As Decimal)           hiddenSalary = value        End Set     End Property 

  • Typically, arglist is only used in the case of property arrays. This entry's example shows such a use for the Wage property.

  • The class constructor is often used to set properties to their initial values.

Example

The example code illustrates a class that has a simple property and a property array.

     Public Enum WageCategory        Rate = 0        Overtime = 1        Differential = 2     End Enum     Public Class Employee        ' ----- Simple class with two public properties.        Dim empName As String        Dim wagesByType(0 To 2) As Decimal        Public Property Name(  ) As String           Get              Return empName           End Get           Set(ByVal value As String)              empName = value           End Set        End Property        Public Property Wage(wageType As WageCategory) As Decimal           Get              Return wagesByType(wageType)           End Get           Set(ByVal value As Decimal)              wagesByType(wageType) = value           End Set        End Property     End Class     Module GeneralCode        Public Sub TestEmployeeClass(  )           Dim oneEmployee As New Employee           oneEmployee.Name = "Bill"           oneEmployee.Wage(WageCategory.Rate) = 15@           oneEmployee.Wage(WageCategory.Overtime) = 15@ * 1.5@           oneEmployee.Wage(WageCategory.Differential) = 15@ * 0.1@.1@           Console.WriteLine(oneEmployee.Name)           Console.Writeline(oneEmployee.Wage(WageCategory.Rate))           oneEmployee = Nothing        End Sub     End Module 

Version Differences

  • VB 6 includes Property Get and Property Set features for its classes, but the syntax used in VB 6 is significantly different from the syntax used in .NET.

  • VB 6 support distinct Property Set and Property Let accessors. VB under .NET eliminates the Property Let accessor.

  • Visual Basic 2005 adds subAccessModifier to the Get and Set block definitions, allowing separate visibility for the two portions of the property.

See Also

Function Statement, Sub Statement




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net