Property Get Statement

Named Arguments

No

Syntax

[Public | Private | Friend] [Static] Property Get name _
						
						 [(arglist)] [As type[()]]
    [statements]
    [name = expression]
    [Exit Property] 
    [statements]
    [name = expression]
End Property


Public

Use: Optional

Type: Keyword

Gives the property scope through all procedures in all modules in the project. If used within a createable class module, the property is also accessible from outside the project. Public, Private, and Friend are mutually exclusive.


Private

Use: Optional

Type: Keyword

Restricts the scope of the property to those procedures within the same module. Public, Private, and Friend are mutually exclusive.


Friend

Use: Optional

Type: Keyword

Only valid in a class module, it gives the property scope to all modules within a project, but not to modules outside the project. Public, Private, and Friend are mutually exclusive.


Static

Use: Optional

Type: Keyword

Preserves the value of variables declared inside the property between calls to the property.


name

Use: Required

The name of the property.


arglist

Use: Optional

Data Type: Any

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


type

Use: Optional

The return data type of the property.


()

Use: Optional

Indicates that the property returns an array. As of VB6, Property Get procedures can return arrays.


statements

Optional

Program code to be executed within the property.


expression

Optional

Data Type: Any

The value to return from the property to the calling procedure.


arglist has the following syntax:

[Optional] [ByVal | ByRef] [ParamArray] varname[( )] _
           [As type] [= defaultvalue]


Optional

Use: Optional

Indicates the argument is optional. An optional argument is one that need not be supplied when calling the property. However, all arguments following an optional one must also be optional. A ParamArray argument can't be optional.


ByVal

Use: Optional

The argument is passed by value; that is, a local copy of the variable is assigned the value of the argument.


ByRef

Use: Optional

The argument is passed by reference; that is, the local variable is simply a reference to the argument being passed. All changes made to the local variable are reflected in the calling argument. ByRef is the default method of passing variables.


ParamArray

Use: Optional

Indicates that the argument is an optional array of variants containing an arbitrary number of elements. It can be used only as the last element of the argument list, and it can't be used with the ByRef, ByVal, or Optional keywords.


varname

Use: Required

The name of the local variable containing either the reference or value of the argument.


type

Use: Optional

The data type of the argument.


defaultvalue

Use: Optional

For optional arguments, you can specify a constant default value.

Description

Declares the name, arguments, and code for a procedure that reads the value of a property and returns it to the calling procedure.

Rules at a Glance

  • Property procedures are Public by default.

  • In VBA applications the Option Private Module statement restricts the scope of a procedure defined as public to the project in which it was defined.

  • 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 aren't listed in the class library for that project. Therefore, they can't be accessed from projects or applications outside the defining application.

  • Properties and procedures defined using the Friend keyword can't be late-bound.

  • The Static keyword only affects variables declared within the Property Get procedure. If you don't use the Static keyword, the values of all local variables are lost between calls.

  • Unlike other function and procedure names, the name of the Property Get procedure doesn't have to be unique within its class module. Specifically, the Property Let and Property Set procedures can have the same name as the Property Get procedure. For example:

    Property Let Name(sVal as String)
        msName = sVal
    End Property
    
    Property Get Name() as String
        Name = msName
    End Property

  • The number and data types of the arguments passed to a Property Get statement must match the corresponding Property Let or Property Set statement. For example:

    Public Property Let MyProperty(sVal As String, iVal As Integer)
        miMyProperty = iVal
    End Property
    
    Public Property Get MyProperty(sVal As String) As Integer
        MyProperty = miMyProperty
    End Property

    Both the Property Let and Property Get procedures share a common argument, sVal. The Property Let procedure has one additional argument, iVal, which represents the value that is to be assigned to the MyProperty property. (For details, see the next point.)

  • In a Property Let procedure, the last argument defines the data type for the property. Therefore, the return data type of the Property Get procedure must match the last argument of any corresponding Property Let or Property Set procedure.

  • type may be Byte, Boolean, Currency, Date, Double, Integer, Long, Object, Single, String, Variant, a user-defined type, or an object type.

  • Fixed-length strings can't be used for type.

  • From VB6 onward, type can be an array of any type.

  • VB6 introduces the concept of remote user-defined types. Before VB6, type could only be a user-defined type if the property was Private. Now UDTs can be passed as Public variables or as the return values of Public properties. However, this requires that either NT Service Pack 4 or the latest DCOM95 patch has been applied. For details, see Chapter 4.

  • 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 Get procedure.

  • If the value of the Property Get procedure has not been explicitly set when the program execution exits the procedure, its value is the uninitialized value of the return data type, as shown in the following table:

    Data Type Initial Value
    Numeric 0
    Variable-length string Zero-length string ("")
    Variant Empty
    Object Nothing
    Date Saturday 30 December 1899 12:00:00

Programming Tips and Gotchas

  • You can create a read-only property by defining a Property Get procedure without a corresponding Property Let or Property Set procedure.

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

    'Class Module Declarations Section
    'private data member only accessable from within 
    'this code module
    Private miMyProperty As Integer
    
    Public Property Let MyProperty(iVal As Integer)
        'procedure to allow the outside world to
        'change the value of private data member
        miMyProperty = iVal
        '(do not use a Property Let when creating a 
        'Read-Only Property)
    End Property
    
    Public Property Get MyProperty() As Integer
        'procedure to allow the outside world to
        'read the value of private data member
        MyProperty = miMyProperty
    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.

See Also

Property Let Statement, Property Set Statement, Section 4.1.1
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.119.229