Поделиться через


Property Get Statement

Declares, in a Class block, the name, arguments, and code that form the body of a Property procedure that gets (returns) the value of a property.

[Public [Default] | Private] Property Get name [(arglist)]
   [statements]
   [[Set] name = expression]
   [Exit Property] 
   [statements]
   [[Set] name = expression]
End Property 

Arguments

  • Public
    Indicates that the Property Get procedure is accessible to all other procedures in all scripts.

  • Default
    Used only with the Public keyword to indicate that the property defined in the Property Get procedure is the default property for the class.

  • Private
    Indicates that the Property Get procedure is accessible only to other procedures in the Class block where it's declared.

  • name
    Name of the Property Get procedure; follows standard variable naming conventions, except that the name can be the same as a Property Let or Property Set procedure in the same Class block.

  • arglist
    List of variables representing arguments that are passed to the Property Get procedure when it is called. Commas separate multiple arguments. The name of each argument in a Property Get procedure must be the same as the corresponding argument in a Property Let procedure (if one exists).

  • statements
    Any group of statements to be executed within the body of the Property Get procedure.

  • Set
    Keyword used when assigning an object as the return value of a Property Get procedure.

  • expression
    Return value of the Property Get procedure.

Remarks

If not explicitly specified using either Public or Private, Property Get procedures are public by default, that is, they are visible to all other procedures in your script. The value of local variables in a Property Get procedure is not preserved between calls to the procedure.

You can't define a Property Get procedure inside any other procedure (e.g. Function or Property Let).

The Exit Property statement causes an immediate exit from a Property Get procedure. Program execution continues with the statement that follows the statement that called the Property Get procedure. Any number of Exit Property statements can appear anywhere in a Property Get procedure.

Like a Sub and Property Let procedure, a Property Get procedure is a separate procedure that can take arguments, perform a series of statements, and change the value of its arguments. However, unlike a Sub and Property Let, you can use a Property Get procedure on the right side of an expression in the same way you use a Function or property name when you want to return the value of a property.

Example

The following example illustrates the use of the Property Get statement.

Class Customer
    Private m_CustomerName
    Private m_Address(2)

    Private Sub Class_Initialize
        m_CustomerName = ""
        m_Address(0) = ""
        m_Address(1) = ""
    End Sub

    ' CustomerName property.
    Public Property Get CustomerName
        CustomerName = m_CustomerName
    End Property

    Public Property Let CustomerName(custname)
        m_CustomerName = custname
    End Property

    ' Address property.
    Public Property Get Address(index)
        Address = m_Address(index)
    End Property

    Public Property Let Address(index, addr)
        m_Address(index) = addr
    End Property
End Class

Dim cust
Set cust = New Customer

' Set the object's properties.
cust.CustomerName = "Fabrikam, Inc."
cust.Address(0) = "4567 Main Street"
cust.Address(1) = "Suite 100"

' Get the object's properties.
Dim s
s = s & cust.CustomerName & vbCrLf
s = s & cust.Address(0) & vbCrLf
s = s & cust.Address(1) & vbCrLf

MsgBox (s)

Requirements

Version 5

See Also

Reference

Class Statement (VBScript)

Dim Statement

Exit Statement

Function Statement (VBScript)

Private Statement

Property Let Statement

Property Set Statement

Public Statement

Set Statement

Sub Statement

Change History

Date

History

Reason

April 2009

Added example.

Customer feedback.