Sideway from Sideway
Draft for Information Only


VB.NET Variable Declaration
 Declaration Levels
  Local and Member Variables
  Shared and Instance Variables
 Declaring Data Type
 Local Type Inference
 Characteristics of Declared Variables
 See also
  How to: Create a New Variable
  To create a new variable
 See also
  How to: Create a Variable that Does Not Change in Value
  To create a variable that does not change in value
 See also
  How to: Move Data Into and Out of a Variable
 Putting Data in a Variable
   To store a value in a variable
 Getting Data from a Variable
   To retrieve a value from a variable
 See also

VB.NET Variable Declaration

You declare a variable to specify its name and characteristics. The declaration statement for variables is the Dim Statement. Its location and contents determine the variable's characteristics.

For variable naming rules and considerations, see Declared Element Names.

Declaration Levels

Local and Member Variables

A local variable is one that is declared within a procedure. A member variable is a member of a Visual Basic type; it is declared at module level, inside a class, structure, or module, but not within any procedure internal to that class, structure, or module.

Shared and Instance Variables

In a class or structure, the category of a member variable depends on whether or not it is shared. If it is declared with the Shared keyword, it is a shared variable, and it exists in a single copy shared among all instances of the class or structure.

Otherwise it is an instance variable, and a separate copy of it is created for each instance of the class or structure. A given copy of an instance variable is available only to the instance of the class or structure in which it was created. It is independent of a copy of the instance variable in any other instance of the class or structure.

Declaring Data Type

The As clause in the declaration statement allows you to define the data type or object type of the variable you are declaring. You can specify any of the following types for a variable:

  • An elementary data type, such as Boolean, Long, or Decimal

  • A composite data type, such as an array or structure

  • An object type, or class, defined either in your application or in another application

  • A .NET Framework class, such as Label or TextBox

  • An interface type, such as IComparable or IDisposable

You can declare several variables in one statement without having to repeat the data type. In the following statements, the variables i, j, and k are declared as type Integer, l and m as Long, and x and y as Single:

Dim i, j, k As Integer  
' All three variables in the preceding statement are declared as Integer.  
Dim l, m As Long, x, y As Single  
' In the preceding statement, l and m are Long, x and y are Single.  

For more information on data types, see Data Types. For more information on objects, see Objects and Classes and Programming with Components.

Local Type Inference

Type inference is used to determine the data types of local variables declared without an As clause. The compiler infers the type of the variable from the type of the initialization expression. This enables you to declare variables without explicitly stating a type. In the following example, both num1 and num2 are strongly typed as integers.

Public Sub inferenceExample()

    ' Using explicit typing.
    Dim num1 As Integer = 3

    ' Using local type inference.
    Dim num2 = 3

End Sub

If you want to use local type inference, Option Infer must be set to On. For more information, see Local Type Inference and Option Infer Statement.

Characteristics of Declared Variables

The lifetime of a variable is the period of time during which it is available for use. In general, a variable exists as long as the element that declares it (such as a procedure or class) continues to exist. If the variable does not need to continue existing beyond the lifetime of its containing element, you do not need to do anything special in the declaration. If the variable needs to continue to exist longer than its containing element, you can include the Static or Shared keyword in its Dim statement. For more information, see Lifetime in Visual Basic.

The scope of a variable is the set of all code that can refer to it without qualifying its name. A variable's scope is determined by where it is declared. Code located in a given region can use the variables defined in that region without having to qualify their names. For more information, see Scope in Visual Basic.

A variable's access level is the extent of code that has permission to access it. This is determined by the access modifier (such as Public or Private) that you use in the Dim statement. For more information, see Access levels in Visual Basic.

See also

How to: Create a New Variable

You create a variable with a Dim Statement.

To create a new variable

  1. Declare the variable in a Dim statement.

    Dim newCustomer
  2. Include specifications for the variable's characteristics, such as Private, Static, Shadows, or WithEvents. For more information, see Declared Element Characteristics.

    Public Static newCustomer

    You do not need the Dim keyword if you use other keywords in the declaration.

  3. Follow the specifications with the variable's name, which must follow Visual Basic rules and conventions. For more information, see Declared Element Names.

    Public Static newCustomer
  4. Follow the name with the As clause to specify the variable's data type.

    Public Static newCustomer As Customer

    If you do not specify the data type, it uses the default: Object.

  5. Follow the As clause with an equal sign (=) and follow the equal sign with the variable's initial value.

    Visual Basic assigns the specified value to the variable every time it runs the Dim statement. If you do not specify an initial value, Visual Basic assigns the default initial value for the variable's data type when it first enters the code that contains the Dim statement.

    If the variable is a reference type, you can create an instance of its class by including the New Operator keyword in the As clause. If you do not use New, the initial value of the variable is Nothing.

    Public Static newCustomer As New Customer

See also

How to: Create a Variable that Does Not Change in Value

The notion of a variable that does not change its value might appear to be contradictory. But there are situations when a constant is not feasible and it is useful to have a variable with a fixed value. In such a case you can define a member variable with the ReadOnly keyword.

You cannot use the Const Statement to declare and assign a constant value in the following circumstances:

  • The Const statement does not accept the data type you want to use

  • You do not know the value at compile time

  • You are unable to compute the constant value at compile time

To create a variable that does not change in value

  1. At module level, declare a member variable with the Dim Statement, and include the ReadOnly keyword.

    Dim ReadOnly timeStarted

    You can specify ReadOnly only on a member variable. This means you must define the variable at module level, outside of any procedure.

  2. If you can compute the value in a single statement at compile time, use an initialization clause in the Dim statement. Follow the As clause with an equal sign (=), followed by an expression. Be sure the compiler can evaluate this expression to a constant value.

    Dim ReadOnly timeStarted As Date = Now

    You can assign a value to a ReadOnly variable only once. Once you do so, no code can ever change its value.

    If you do not know the value at compile time, or cannot compute it at compile time in a single statement, you can still assign it at run time in a constructor. To do this, you must declare the ReadOnly variable at class or structure level. In the constructor for that class or structure, compute the variable's fixed value, and assign it to the variable before returning from the constructor.

See also

How to: Move Data Into and Out of a Variable

You store a value in a variable by putting the variable name on the left side of an assignment statement.

Putting Data in a Variable

To store a value in a variable

  • Use the variable name on the left side of an assignment statement.

    The following example sets the value of the variable alpha.

    alpha = (beta * 6.27) / (gamma + 2.1)

    The value generated on the right side of the assignment statement is stored in the variable.

Getting Data from a Variable

You retrieve a variable's value by including the variable name in an expression.

To retrieve a value from a variable

  • Use the variable name in an expression. You can use a variable anywhere you can use a constant or a literal, except in an expression that defines the value of a constant.


  • Use the variable name following the equal (=) sign in an assignment statement.

    The following example reads the value of the variable startValue and then uses the value of the variable counter in an expression.

    counter = startValue
    cellValue = (counter + 5) ^ 2

    The value of the variable participates in the expression just as a constant would, and then it is stored in the variable or property on the left side of the assignment statement.

See also



ID: 200900006 Last Updated: 9/6/2020 Revision: 0 Ref:



  1. Active Server Pages,  ,
  2. ASP Overview,  ,
  3. ASP Best Practices,  ,
  4. ASP Built-in Objects,  ,
  5. Response Object,  ,
  6. Request Object,  ,
  7. Server Object (IIS),  ,
  8. Application Object (IIS),  ,
  9. Session Object (IIS),  ,
  10. ASPError Object,  ,
  11. ObjectContext Object (IIS),  ,
  12. Debugging Global.asa Files,  ,
  13. How to: Debug Global.asa files,  ,
  14. Calling COM Components from ASP Pages,  ,
  15. IIS ASP Scripting Reference,  ,
  16. ASP Keywords,  ,
  17. Creating Simple ASP Pages,  ,
  18. Including Files in ASP Applications,  ,
  19. ASP Overview,  ,
  20. FileSystemObject Object,  ,
  21.,  , ADO Object Model
  22. ADO Fundamentals,  ,

Latest Updated LinksValid XHTML 1.0 Transitional Valid CSS!Nu Html Checker Firefox53 Chromena IExplorerna

Home 5






Hobbies 8


Chinese 1097

English 337

Reference 68


Hardware 154


Application 207

Digitization 25

Latex 35

Manim 202

Numeric 19


Web 285

Unicode 504


CSS 60


OS 422

DeskTop 7

Python 61



Formulas 8

Algebra 65

Number Theory 206

Trigonometry 18

Geometry 22

Coordinate Geometry 1

Calculus 67

Complex Analysis 21


Tables 8


Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5


Process Control 1

Acoustics 19

FiniteElement 2

Natural Sciences

Matter 1

Electric 27

Biology 1

Geography 1

Copyright © 2000-2021 Sideway . All rights reserved Disclaimers last modified on 06 September 2019