Public [WithEvents] varname[([subscripts])] [As [New] type][= initexpr] [,[WithEvents] varname[([subscripts])] [As [New] type][= initexpr]] . . .
The Public statement syntax has these parts:
Part | Description |
---|---|
WithEvents | Optional. Keyword specifying that varname is an object variable used to respond to events triggered by an ActiveX object. You can declare as many individual variables as you like using WithEvents, but you can't create arrays with WithEvents. You can't use New with WithEvents. |
varname | Required. Name of the variable; follows standard variable naming conventions. |
subscripts | Optional. Dimensions of an array variable; up to 60 multiple dimensions may be declared. The subscripts argument uses the following syntax for arrays where both the lower and upper bounds are specified:
[lower To] upper [,[lower To] upper] . . . For fixed-rank arrays. the lower bound value of the array is not specified and the * character is used in place of the upper bound value. |
New | Optional. Keyword that enables implicit creation of an object. If you use New when declaring the object variable, a new instance of the object is created on first reference to it, so you don't have to use the Set statement to assign the object reference. The New keyword can't be used to declare variables of any intrinsic data type, can't be used to declare instances of dependent objects, and can't be used with WithEvents. |
type | Optional. Data type of the variable; may be Byte, Boolean, Char, Short, Integer, Long, Single, Double, Decimal, Date, String, (for variable-length strings), String * length (for fixed-length strings), Object, a user-defined type, or an object type. Use a separate As type clause for each variable being defined. |
initexpr | Optional. Any expression that provides the initial value to assign to the variable when its declared. A variable cannot be initialized using this syntax if an As New clause is used. |
Variables declared using the Public statement are available to all procedures in all classes, modules and structures in all applications.
Use the Public statement to declare the data type of a variable. For example, the following statement declares a variable as an Integer and initializes the value to 10.
Public NumberOfEmployees As Integer = 10
Also use a Public statement to declare the object type of a variable. The following statement declares a variable for a new instance of a worksheet.
Public X As New Worksheet
If the New keyword is not used when declaring an object variable, the variable that refers to the object must be assigned an existing object using the Set statement before it can be used. Until it is assigned an object, the declared object variable has the special value Nothing, which indicates that it doesn't refer to any particular instance of an object.
You can initialize the values of a fixed-size or fixed-rank array by surrounding the initialization values with curly braces. The following example creates a fixed-size array with four elements:
Dim A(3) as Integer = {0, 1, 2, 3}
The following example creates a fixed-rank array and initializes 3 elements:
Dim A(*) As Integer = {10, 20, 30}
You can also use the Public statement with empty parentheses to declare a dynamic array. You then initialize the values of the array in the same way as for fixed-size arrays. The following example initializes values for a dynamic array:
Dim A() as Integer = {0, 1, 2, 3}
If you want to initialize a dynamic array with any lower bound other than the default (either 0 or 1), you must use the ReDim statement. For example:
ReDim A(2 To 5) as Integer = {0, 1, 2, 3}
For multi-dimensional arrays, each separate dimension is included in curly braces. The elements are listed in row-major order. For example:
Initialize array elements to their row index * their column index Dim a(1 to 2, 3 to 5) As Integer = {{1*3, 1*4, 1*5},{2*3 ,2*4, 2*5}}
After declaring a dynamic array, use the ReDim statement within a procedure to define the number of dimensions and elements in the array. If you try to redeclare a dimension for an array variable whose size was explicitly specified in a Private, Public, Protected or Dim statement, an error occurs.
If you don't specify a data type or object type the variable is Object by default.
When no initialization value is specified, a numeric variable is initialized to 0, a variable-length string is initialized to a zero-length string (""), and a fixed-length string is filled with zeros. Object variables are initialized toEmpty. Each element of a user-defined type variable is initialized as if it were a separate variable.
Array Function | Const Statement | Dim Statement | Private Statement | ReDim Statement