Sideway
output.to from Sideway
Draft for Information Only

Content

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
  Source/Reference

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.

VB
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.

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

    VB
    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.

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

    VB
    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.

    VB
    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.

    VB
    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.

    VB
    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.

    VB
    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.

    -or-

  • 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.

    VB
    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

Source/Reference


©sideway

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

close

References

  1. Active Server Pages,  , http://msdn.microsoft.com/en-us/library/aa286483.aspx
  2. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929%28v=vs.90%29.aspx
  3. ASP Best Practices,  , http://technet.microsoft.com/en-us/library/cc939157.aspx
  4. ASP Built-in Objects,  , http://msdn.microsoft.com/en-us/library/ie/ms524716(v=vs.90).aspx
  5. Response Object,  , http://msdn.microsoft.com/en-us/library/ms525405(v=vs.90).aspx
  6. Request Object,  , http://msdn.microsoft.com/en-us/library/ms524948(v=vs.90).aspx
  7. Server Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525541(v=vs.90).aspx
  8. Application Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525360(v=vs.90).aspx
  9. Session Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms524319(8v=vs.90).aspx
  10. ASPError Object,  , http://msdn.microsoft.com/en-us/library/ms524942(v=vs.90).aspx
  11. ObjectContext Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525667(v=vs.90).aspx
  12. Debugging Global.asa Files,  , http://msdn.microsoft.com/en-us/library/aa291249(v=vs.71).aspx
  13. How to: Debug Global.asa files,  , http://msdn.microsoft.com/en-us/library/ms241868(v=vs.80).aspx
  14. Calling COM Components from ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524620(v=VS.90).aspx
  15. IIS ASP Scripting Reference,  , http://msdn.microsoft.com/en-us/library/ms524664(v=vs.90).aspx
  16. ASP Keywords,  , http://msdn.microsoft.com/en-us/library/ms524672(v=vs.90).aspx
  17. Creating Simple ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524741(v=vs.90).aspx
  18. Including Files in ASP Applications,  , http://msdn.microsoft.com/en-us/library/ms524876(v=vs.90).aspx
  19. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929(v=vs.90).aspx
  20. FileSystemObject Object,  , http://msdn.microsoft.com/en-us/library/z9ty6h50(v=vs.84).aspx
  21. http://msdn.microsoft.com/en-us/library/windows/desktop/ms675944(v=vs.85).aspx,  , ADO Object Model
  22. ADO Fundamentals,  , http://msdn.microsoft.com/en-us/library/windows/desktop/ms680928(v=vs.85).aspx
close
IMAGE

Home 5

Business

Management

HBR 3

Information

Recreation

Hobbies 8

Culture

Chinese 1097

English 337

Reference 67

Computer

Hardware 149

Software

Application 198

Digitization 25

Latex 6

Manim 113

Numeric 19

Programming

Web 283

Unicode 494

HTML 65

CSS 58

ASP.NET 145

OS 389

DeskTop 7

Python 19

Knowledge

Mathematics

Formulas 8

Algebra 25

Number Theory 206

Trigonometry 18

Geometry 21

Calculus 67

Complex Analysis 21

Engineering

Tables 8

Mechanical

Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5

Control

Process Control 1

Acoustics 19

FiniteElement 2

Physics

Electric 27

Biology 1

Geography 1


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