How to: Define and Access a Variable in VB.NET

A variable corresponds to a location of memory, which stores values when performing calculations.
Variables are used in calculations, comparisons. They have established a name in the source program, and accessing the value stored in memory location is achieved by variable name.

In VB.NET, a variable has an associated data type […], without being required to define the association at the definition time. In the latter case, the data type associated to the variable is deducted from the value of initialization.
Storing a value in memory location regarding a variable is done by including the variable in assignment operations.
Categories of variables:

  • members: defined in modules, classes of objects, structures;
  • locals: defined in sub-routines.

VB.NET variables are defined using the clause Dim. The association of a data type to a variable is performed with clause As.

Dim a As Integer = 10
Dim b = 15
Dim d, e As Boolean, f, g As Long

In the example above, variables a, d, e, f and g are defined having associated data types. Variable b is not explicitly associated with the data type, which is set by Integer through initialization with value 15.
Definition and reference multiple memory locations using a single variable is achieved by arrays, matrixes and so forth. Access a value of a massive is achieved by an index. A massive variable is associated with a number of sizes. Dimensions are “directions” filled by values. Storage capacity of a massive in VB.NET is determined as a multiplication scale of its sizes incremented by value of 1, beeing expressed in number of elements.
Definition of massive variables and reference of their elements are achived as follows:

Dim vector(10) As Integer
a = b + vector(0)
Dim mat(10,10) As Double
a = a + mat(0,0)

In the example above, the storage capacity of uni-dimensional massive vector is 11 elements, respectively 121 elements (11 lines x 11 columns) for two-dimensional massive mat. Each element of the vector has the data type Integer, and each element of the mat is associated with data type Double.
Expression vector(0) extracts the first element of the massive vector and the expression mat(0,0) refers the item positioned on the line 1, column 1 of the massive mat.
Dim clause to define a variable in VB.NET can be preceded by an access modifier to determine the level of access from source code to defined variable. Access modifiers and their significance are:

  • Public: access is provided, beeing accessible by any source code;
  • Protected: restricted access for source code developed outside class of objects […] or classes derived from it and that member variable was defined;
  • Friend: access granted to code developed in the same structural entity of the program;
  • Protected Friend: unified modifiers Friend and Protected;
  • Private: access granted only to code included in the data type defined.

Other optional elements to define a variable:

  • Shared: defined element (variable) is associated with a class or structure, not to an instance;
  • Shadows: defined element (variable) redefines or hides an item or many items are overloaded;
  • Static: variables defined locally continue their existence after sub-routine finish where they have been defined, including the values associated to them;
  • ReadOnly: defined element (variable) can only be read, not written;
  • WithEvents: it is specific to object variables defined on classes of objects that deal with events […]; it states before the object variable name or a list of object variables.