Syntax
|
Public
name [(subscripts)] [
As
type] [
,name [
(subscripts)
] [
As
type]]...
|
Description
|
Declares a list of public variables and their corresponding types and sizes.
|
Comments
|
Public variables are global to all
Sub
s and
Function
s in all scripts.
If a type-declaration character is used when specifying name (such as
%
,
@
,
&
,
$
, or
!
), the optional
[
As
type
]
expression is not allowed. For example, the following are allowed:
Public foo As Integer
Public foo%
The subscripts parameter allows the declaration of arrays. This parameter uses the following syntax:
[lower To] upper [,[lower To] upper]...
The lower and upper parameters are integers specifying the lower and upper bounds of the array. If lower is not specified, then the lower bound as specified by Option Base is used (or 1 if no Option Base statement has been encountered). Up to 60 array dimensions are allowed.
|
|
The total size of an array (not counting space for strings) is limited to 64K.
Dynamic arrays are declared by not specifying any bounds:
Public a()
The type parameter specifies the type of the data item being declared. It can be any of the following data types: String, Integer, Long, Single, Double, Currency, Object, data object, built-in data type, or any user-defined data type.
If a variable is seen that has not been explicitly declared with either
Dim
,
Public
, or
Private
, then it will be implicitly declared local to the routine in which it is used.
For compatibility, the keyword
Global
is also supported. It has the same meaning as
Public
.
Fixed-Length Strings
Fixed-length strings are declared by adding a length to the
String
type-declaration character:
Public name As String * length
where length is a literal number specifying the string's length.
Initial Values
All declared variables are given initial values, as described in the following table:
|
|
Parameter
|
Description
|
|
Integer
|
0
|
|
Long
|
0
|
|
Double
|
0.0
|
|
Single
|
0.0
|
|
Currency
|
0.0
|
|
Date
|
December 31, 1899 00:00:00
|
|
Object
|
Nothing
|
|
Boolean
|
False
|
|
Variant
|
Empty
|
|
String
|
"" (zero-length string)
|
|
User-defined type
|
Each element of the structure is given a default value, as described above.
|
|
Arrays
|
Each element of the array is given a default value, as described above.
|
|
Sharing Variables
When sharing variables, you must ensure that the declarations of the shared variables are the same in each script that uses those variables. If the public variable being shared is a user-defined structure, then the structure definitions must be exactly the same.
|
Example
|
This example uses a subroutine to calculate the area of ten circles and displays the result in a dialog box. The variables R and Ar are declared as Public variables so that they can be used in both Main and Area.
Const crlf = Chr$(13) + Chr$(10)
Public x#,ar#
Sub Area()
ar# = (x# ^ 2) * Pi
End Sub
Sub Main()
msg1 = "The area of the ten circles are:" & crlf & crlf
For x# = 1 To 10
Area
msg1 = msg1 & x# & ": " & Format(ar#,"fixed") & Basic.Eoln$
Next x#
MsgBox msg1
End Sub
|
See Also
|
Dim (statement); Redim (statement); Option Base (statement).
|