Public Statement

Named Arguments

No

Syntax

Public [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, which must follow Visual Basic naming conventions.


subscripts

Use: Optional

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


New

Use: Optional

Type: Keyword

Used to automatically create 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 public variable and allocate the relevant storage space in memory. A Public variable has both project-level scope—that is, it can be used by all procedures in all modules in the project—and, when used in a class module, it can have scope outside the project.

Rules at a Glance

  • The behavior of a Public variable depends on where it's declared, as the following table shows:

    Variable Declared in... Scope
    Any procedure Illegal; generates a compile-time error.
    Code module declarations section Variable is available to all modules within the project.
    Class module declarations section Variable is available as a property of the class to all modules within the project, and to all other projects referencing the class.
    Form module declarations section Variable is available as a property of the form to all modules within the project.

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

  • WithEvents is only valid when used to declare an object variable.

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

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

  • The subscripts argument has the following syntax:

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

  • Using the subscripts argument, you can declare up to 60 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 present, the lower bound of the array is zero.

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

  • The New keyword 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 negates the need to use the Set statement.

  • You can't use the New keyword to declare any of the following: variables of any intrinsic data type (the New keyword is for use with object variables only), instances of dependent objects (a dependent object is one that can only be created from a method or property in another object; a dependent object isn't publicly createable), a variable that uses 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.

  • type 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 type, 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:

    As String * stringlength

    where stringlength is the number of characters to allocate to the string.

  • You can't use the Public statement to declare a fixed-length string variable in a class module.

Programming Tips and Gotchas

  • Instead of declaring a variable as Public within either a form or class module, you should create Property Let and Property Get sub procedures that assign and retrieve the value of a private variable, respectively.

  • In VBA applications, WithEvents is valid only in class modules; however, standalone versions of VB allow the use of WithEvents all object modules.

  • A new type of scope has been 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.

  • You should note that when you use the New keyword, 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 causing hard-to-find errors.

  • You may have occasion to maintain legacy Visual Basic code 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 carry the prefix keyword Global are similar to Public variables.

See Also

Friend Statement, Option Private Module Statement, Private Statement, ReDim Statement, Set Statement
..................Content has been hidden....................

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