Static Statement

   
Static Statement

Syntax

 Static   varname   [([   subscripts   ])] [As [New]   type   ] _        [,   varname   [([   subscripts   ])] [As [New]   type   ]] . . . 
varname (required; any)

The name of the variable, following Visual Basic naming conventions

subscripts (optional; Integer)

Denotes varname as an array and specifies the dimension and upper bounds of the array

New (optional; Keyword)

Used to automatically create an instance of the object referred to by the object variable, varname

type (optional; Keyword)

Data type of the variable varname

Description

Used at procedure level to declare a Static variable and to allocate the relevant storage space in memory. Static variables retain their value between calls to the procedure in which they are declared.

Rules at a Glance

  • A Static variable's scope is limited to the procedure in which it is created.

  • The subscripts argument has the following syntax:

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

  • The New keyword specifies that a new instance of the object will be created. Use of the New keyword in the Static statement therefore eliminates the subsequent need to instantiate the object.

  • You cannot use the New keyword to declare variables of any intrinsic data type or to declare instances of dependent objects.

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

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

  • If you don't specify datatype , the variable will be cast as an Object.

  • When multiple variables are declared on the same line, if a variable is not declared with a explicit type declaration, then its type is that of the next variable with an explicit type declaration. Thus, in the line:

     Static   x   As Long,   i   ,   j   ,   k   As Integer,   s   As String 

    the variables i , j , and k have type Integer. (In VB 6, the variables i and j would have type Variant.)

  • When a static variable is initialized on the same line as its declaration, the initialization process is performed only the first time the declaration line is encountered . (Otherwise, the variable would not be static.)

  • VB.NET permits the initialization of variables in the same line as their declaration (at long last!). Thus, we may write:

     Static   x   As Integer = 5 

    to declare an Integer variable and initialize it to 5. Similarly, we can declare and initialize more than one variable on a single line:

     Static   x   As Integer = 6,   y   As Integer = 9 
  • Variables that are not explicitly initialized by the Static statement have the following default values:

Data type

Initial value

All numeric types

Boolean

False

Date

01/01/0001 12:00:00 AM

Decimal

Object

Nothing

String

Zero-length string ("")

  • Static variables can have procedure-level scope or block-level scope. Static variables with procedure-level scope last the lifetime of the application, but they are accessible only within the procedure in which they are defined. Static variables with block-level scope last the lifetime of the application, but they are accessible only within the code block (such as a looping construct or an If statement) in which they are defined.

Programming Tips and Gotchas

  • It is a recognized programming practice when using the Static statement in a procedure to put the Static statement at the beginning of that procedure.

  • Although their value persists between calls to a procedure, Static variables do not have scope outside of the procedure in which they are created.

  • For more on static variables, see Chapter 3.

VB.NET/VB 6 Differences

  • When multiple variables are declared on a single line of code in VB 6, variables not explicitly assigned a data type are cast as variants. For example, in the statement:

     Static   Var1   ,   Var2   ,   Var3   As String 

    both Var1 and Var2 are variants rather than strings. In VB.NET, the type declaration applies to all undeclared variables since the last explicit type declaration. So the previous statement in VB.NET would cast Var1 , Var2 , and Var3 as strings.

  • In VB 6, declaring and initializing variables are separate steps; aside from allowing VB to assign variables their default values, variables cannot be initialized at the same time they are declared. In VB.NET, variables can be assigned an initial value when they are declared.

  • VB 6 allowes you to declare fixed-length strings; they are not supported, however, in VB.NET.

  • VB 6 allows you to define the lower bound of an array when it is initialized. In VB.NET, all arrays have a lower bound of 0. Hence, the VB 6 syntax:

     Static array(1 To 20) As String 

    is not supported in VB.NET.

  • In VB 6, arrays are either fixed length or dynamic; in VB.NET, all arrays are dynamic.

  • In VB 6, it is possible to define a procedure or a function as Static, meaning that all local variables defined in that routine are static. In VB.NET, the use of the Static keyword with the Function or Sub statements is not supported.

See Also

Dim Statement

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net