9.6 Instance and Shared Variables
An instance or shared variable is a member of a type that can store information. The Dim modifier must be specified if no modifiers are specified, but may be omitted otherwise. A single variable declaration may include multiple variable declarators; each variable declarator introduces a new instance or shared member. If an initializer is specified, only one instance or shared variable may be declared by the variable declarator.
A variable declared with the Shared modifier is a shared variable. A shared variable identifies exactly one storage location regardless of the number of instances of the type that are created. A shared variable comes into existence when a program begins executing, and ceases to exist when the program terminates.
A variable declared without the Shared modifier is called an instance variable. Every instance of a class contains a separate copy of all instance variables of the class. An instance variable of a reference type comes into existence when a new instance of that type is created, and ceases to exist when there are no references to that instance and the Finalize method has executed. An instance variable of a value type has exactly the same lifetime as the variable to which it belongs. In other words, when a variable of a value type comes into existence or ceases to exist, so does the instance variable of the value type.
If the declarator contains an As clause, the clause specifies the type of the members introduced by the declaration. If the type is omitted and strict semantics are being used, a compile-time error occurs. Otherwise the type of the members is implicitly Object or the type of the members' type character.
Note There is no ambiguity in the syntax: if a declarator omits a type, it will always use the type of a following declarator.
The accessibility domain of an instance or shared variable's type or array element type must be the same as or a superset of the accessibility domain of the instance or shared variable itself.
The following example shows a
Color class that has internal instance variables named
Class Color Friend redPart As Short Friend bluePart As Short Friend greenPart As Short Public Sub New(red As Short, blue As Short, green As Short) redPart = red bluePart = blue greenPart = green End Sub End Class
VariableMemberDeclaration ::= [ Attributes ] [ VariableModifier+ ] [ Dim ] VariableDeclarators LineTerminator VariableModifier ::= AccessModifier | Shadows | Shared | ReadOnly | WithEvents VariableDeclarators ::= VariableDeclarator | VariableDeclarators , VariableDeclarator VariableDeclarator ::= VariableIdentifiers [ As TypeName ] | VariableIdentifier [ As [ New ] TypeName [ ( ArgumentList ) ] ] [ = VariableInitializer ] VariableIdentifiers ::= VariableIdentifier | VariableIdentifiers , VariableIdentifier VariableIdentifier ::= Identifier [ ArrayNameModifier ]
9.6.1 Read-Only Variables | 9.6.2 WithEvents Variables | 9.6.3 Variable Initializers | 9.2 Methods | 9.3 Constructors | 9.4 Events | 9.5 Constants | 9.7 Properties | 4.6 Accessibility | 4.3.3 Shadowing | Declarations and Constants Summary (Visual Basic Language Reference) | Variables (Visual Basic Language Concepts)