NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Private Statement

Used at module level to declare private variables and allocate storage space.

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

The Private statement syntax has these parts:

Part Description
WithEvents Optional. Keyword that specifies 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 it’s declared. A variable cannot be initialized using this syntax if an “As New” clause is used.

Remarks

Private variables are available only to the class, module or structure in which they are declared.

Use the Private 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.

Private NumberOfEmployees As Integer = 10

You can also use a Private statement to declare the object type of a variable. The following statement declares a variable for a new instance of a worksheet.

Private X As New Worksheet

If the New keyword isn't 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's 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.

If you don't specify a data type or object type, , the variable is Object by default.You can initialize the values of a fixed-size or fixed-rank array by surrounding the initialization values with 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 Private 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.

Note   When you use the Private statement in a procedure, you generally put the Private statement at the beginning of the procedure.

See Also

Example

Array Function | Const Statement | Dim Statement | Function Statement | | ReDim Statement | Sub Statement