Private Statement

Named Arguments

No

Syntax

Private [WithEvents] varname[([subscripts])] [As [New] _
        type] [, [WithEvents] varname[([subscripts])] _ 
        [As [New] type]] . . .


WithEvents

Use: Optional

Type: Keyword

A keyword that denotes the object variable varname can respond to events triggered from within the object to which it refers.


varname

Use: Required

Data Type: Any

The name of the variable, following Visual Basic naming conventions.


subscripts

Use: Optional

Data Type: Integer or Long

Denotes varname as an array and specifies the number and extent of array dimensions.


New

Use: Optional

Type: Keyword

Automatically creates an instance of the object referred to by the object variable varname.


type

Use: Optional

Data type of the variable varname.

Description

Used at module level to declare a private variable and allocate the relevant storage space in memory.

Rules at a Glance

  • A Private variable's scope is limited to the module in which it's created.

  • WithEvents is only valid when used to declare an object variable. The WithEvents keyword informs VB that the object being referenced exposes events. When you declare an object variable using WithEvents, an entry for the object variable is placed in the code window's object list, and a list of the events available to the object variable is placed in its procedures list. You can then write code in the object variable's event handlers in the same way that you write other more common event handlers like Form_Load.

  • There is no limit to the number of object variables that can refer to the same object using the WithEvents keyword; they all respond to that object's events. For example:

    Private WithEvents adrEmployees As ADODB.Recordset
    Private WithEvents adrDepartments As ADODB.Recordset
    
    Private Sub adrDepartments_MoveComplete( _
                ByVal adReason As ADODB.EventReasonEnum, _
                ByVal pError As ADODB.Error, _
                adStatus As ADODB.EventStatusEnum, _
                ByVal pRecordset As ADODB.Recordset)
        'code here....
    End Sub
    
    Private Sub adrEmployees_MoveComplete( _
                ByVal adReason As ADODB.EventReasonEnum, _
                ByVal pError As ADODB.Error, _
                adStatus As ADODB.EventStatusEnum, _
                ByVal pRecordset As ADODB.Recordset)
        'code here....
    End Sub

  • You can't create an array variable that uses the WithEvents keyword.

  • The New keyword can't be used in the same object variable declaration as WithEvents. This is because WithEvents is designed to trap event notifications that would ordinarily be inaccessible to a Visual Basic program. Consequently, WithEvents can define only an instance of an existing object.

  • The subscripts argument has the following syntax:

    [lowerbound To] upperbound [,[lowerbound To] upperbound]

    For example:

    Private strNames(10)

    defines an array of 11 elements (an array whose lower bound is zero, since an explicit lowerbound value isn't provided, and whose upper bound is ten). Similarly:

    Private(lngPrices(1 to 10)

    defines an array of ten elements whose index values range from 1 through 10.

  • Using the subscripts argument, you can declare up to 60 multiple dimensions for the array.

  • The lowerbound argument of the subscripts argument is optional; when not used, the lower bound of the array is specified by the Option Base statement. If Option Base isn't used, the lower bound of the array is 0.

  • If the subscripts argument isn't used (i.e., the variable name is followed by empty parentheses), the array is declared dynamic. You can change both the number of dimensions and number of elements of a dynamic array using the ReDim statement.

  • The New keyword is used only when declaring an object variable and denotes that a new instance of the object is created when the first reference to the object is made. Use of the New keyword therefore eliminates the need to use the Set statement to instantiate the object. For example:

    Private oEmployee As Employee
    Set oEmployee = New Employee

    or:

    Private oEmployee As New Employee

  • The New keyword can be used only with early bound objects; that is, an object that has a reference added to the project using the References dialog.

  • You can't use the New keyword to declare variables of any intrinsic data type, instances of dependent objects, or variables that use the WithEvents argument.

  • If you don't use the New keyword with an object variable, you must use the Set statement to assign an existing object to the variable before you can use it.

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

  • If you don't specify datatype and you haven't used a DefType statement, the variable is cast as a Variant.

  • The following table shows the values held by each data type when a variable is first initialized.

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

  • The individual elements of a user-defined type are initialized with the value corresponding to their data type.

  • To declare a fixed-length string, use the syntax:

    Private stringvar As String * stringlength

Programming Tips and Gotchas

  • All variables created at procedure level are Private by default. That is, they don't have scope outside the procedure in which they are created.

  • In VBA applications, the WithEvents keyword is valid only in class modules. However, standalone versions of VB allow the use of WithEvents in class, form, and other object modules.

  • A new type of scope was introduced in Visual Basic 5.0. The Friend scope is halfway between Public and Private. It's useful in situations where Private is too restricting, and Public is too open. For more information, refer to the Friend statement.

  • Note that when you use the New keyword to declare an object variable, the Initialize event of the object is fired on the first reference to the object, not when the object variable is declared.

  • It's good programming practice to always use Option Explicit at the beginning of a module to prevent misnamed variables from causing hard-to-find errors.

  • You may have occasion to maintain legacy Visual Basic code that was written prior to Version 4, when the Private and Public statements came into the language. In this case, those variables declared in the Declarations section at the start of a code or form module that aren't explicitly defined as global are in fact Private variables.

See Also

Friend Statement, Public Statement, ReDim Statement, Set Statement, WithEvents Keyword
..................Content has been hidden....................

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