Property Statement

   
Property Statement

Syntax

 [Default]  [accessmodifier] [ReadOnly WriteOnly] _       [   ClassBehavior   ] Property   name   _       [(   arglist   )] [As   type   ] [Implements   interfacemember   ]     Get        [   statements   ]     End Get     Set         [   statements   ]     End Set End Property 
Default (optional; Keyword)

Specifies that the property is the default property. Must have both a Get and a Set block.

accessmodifier (optional; Keyword)

One of the keywords Public , Private , Protected , Friend , or Protected Friend . For more information, see Section 4.7 in Chapter 4.

ReadOnly (optional; Keyword)

Indicates that the property is read-only. Must have only a Get block. (If you try to write a Set block, VB will generate a syntax error.)

WriteOnly (optional; Keyword)

Indicates that the property is write-only. Must have only a Set block. (If you try to write a Get block, VB will generate a syntax error.)

ClassBehavior (optional; Keyword)

One of the following keywords:

Overloads

Indicates that more than one declaration of this function exists (with different argument signatures). For more detail, see Chapter 4.

Overrides

For derived classes, indicates that the function overrides the function by the same name (and argument signature) in the base class. For more detail, see Chapter 4.

Overridable

Indicates that the function can be overridden in a derived class. For more detail, see Chapter 4.

NotOverridable

Indicates that the function cannot be overridden in a derived class. For more detail, see Chapter 4.

MustOverride

Indicates that the function must be overridden in a derived class. For more detail, see Chapter 4.

Shadows (optional; Keyword)

Indicates that the property shadows any element of this same name in a base class.

Shared

A shared function is callable without creating an object of the class. It is, in this strange sense, shared by all objects of the class. These are also called static functions.

name (required; String literal)

The name of the property.

arglist (optional; any)

A comma-delimited list of variables to be passed to the property as arguments from the calling procedure.

type (optional)

The return data type of the property. The default is Object.

Implements interfacename (optional)

Indicates that the property implements a property by the same name in the interface named interfacename .

Description

Declares a class property

Rules at a Glance

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

  • Property procedures are Public by default.

  • The Friend keyword is only valid within class modules. Friend procedures are accessible to all procedures in all modules and classes within a project, but are not listed in the class library for that project. Therefore, they cannot be accessed from projects or applications outside the defining application.

  • Properties and procedures defined using the Friend keyword cannot be late bound.

  • The Default keyword can be used only in the case of parameterized properties. Typically, these are properties that either return collection objects or are implemented as property arrays.

  • By default, arguments are passed to the property procedures by value ( ByVal ).

  • type defines not only the data type returned by the property, but also the data type of the value to be assigned to the property.

  • A Property Get procedure is very similar to a function: the value returned by the property is indicated by assigning that value to a variable whose name is the same as the property.

  • In a Property Set procedure, the value being assigned to the property is represented by the keyword Value . Its data type is represented by the As type clause.

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

Programming Tips and Gotchas

  • You should protect the values of properties by defining a Private variable to hold the internal property value and to control the updating of the property by outside applications through the Property statement, as the following template describes:

     ' Salary property is read/write     Private mdecSalary As Decimal     Property Salary(  ) As Decimal         Get             Salary = mdecSalary         End Get         Set             mdecSalary = Value         End Set     End Property 

    Otherwise, if the variable used to store a property value is public, its value can be modified arbitrarily by any application that accesses the class module containing the property.

  • Typically, arglist need be specified only in the case of property arrays. For example:

     Public Class CEmployee Private sPhone(2) As String Property Phone(idx As Integer) As String    Get       Phone = sPhone(idx)    End Get    Set       sPhone(idx) = Value    End Set End Property End Class 
  • The class constructor is typically used to initialize property values to their default settings.

VB.NET/VB 6 Differences

The syntax for declaring properties in VB.NET is significantly different from the syntax in VB 6. Some of the differences include:

  • VB 6 includes individual Property Get (to retrieve a property value), Property Let (to assign a property value), and Property Set (to assign a reference to a property value) statements. VB.NET replaces this with a single Property...End Property construct.

  • In VB 6, all values including the property values themselves passed to property statements are expressed as parameters. In VB.NET, the value to be assigned to a property is represented by the Value keyword, rather than by a formal parameter.

  • In VB 6, because Property Set , Property Let , and Property Get procedures are separate, standalone constructs, it is possible to expose property procedures with mixed visibility (a private Property Let procedure, for example, and a public Property Get procedure). In VB.NET, because the Property statement defines the visibility of the property as a whole, mixed visibility is not supported.

See Also

Get Statement, Set Statement

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

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