VB.Net

VB.Net Variables

VB.Net Variables

A variable is simply the name provided to a storage location that the programs can access. Each variable in VB.Net has a unique type, which governs the size and layout of the variable's memory, the range of values that may be stored inside that memory, and the set of operations that can be performed on the variable.

Data kinds have already been covered. The basic value types given by VB.Net are classified as follows:

Type

Example

Integral types

SByte, Byte, Short, UShort, Integer, UInteger, Long, ULong and Char

Floating-point types

Single and Double

Decimal types

Decimal

Boolean types

True or False values, as assigned

Date types

Date

Variable declaration syntax in VB.Net −

[ < attributelist > ] [ accessmodifier ] [[ Shared ] [ Shadows ] | [ Static ]]

[ ReadOnly ] Dim [ WithEvents ] variablelist

Where 

  • Attributelist: a list of attributes that apply to a variable. It is Optional.
  • Accessmodifier: used to define the access levels, it has values as - Public, Friend, Protected, Protected Friend and Private. It is optional.
  • Shared: used to declare a shared variable that is not linked with any specific structure or instance of a class. It is, in fact, and available when you declare itYou,  when you declare itYou,Microsoft documentation can obtain a when you pass parameters by referenceetcFirst, let ussoft documentation can obtain an all the structures or the instances of the class. Optional.
  • Shadows: show that the variable re-declares and conceals an identically named element, or collection of overloaded elements, in a base class. Optional.
  • Static: indicates that the variable will keep its value even after the termination of the method in which it is declared. Optional.
  • ReadOnly: defines a variable that can only be read, not written. Optional.
  • WithEvents: indicates that the variable will be used to respond to events raised by the instance to which the variable has been assigned. Optional.
  • Variablelist: gives the list of declared variables.

Variable Initialization in VB.Net

With an equal sign followed by a constant expression, variables are initialised (given a value). The most common type of initialization is −

variable_name = value;

Accepting Values from User

The Console class in the System namespace has a function ReadLine that accepts user input and stores it in a variable. For example,

Dim message As String

message = Console.ReadLine

Lvalues and Rvalues

There are two types of expressions −

  • lvalue − An lvalue expression can appear on either the left or right side of an assignment.
  • rvalue − An rvalue expression may occur on the right-hand side of an assignment but not on the left.

Variables are lvalues; therefore, they can appear on the left side of an assignment. Because numerical literals are rvalues, they cannot be allocated and appear on the left-hand side. The following is a correct statement:

Dim g As Integer = 40

However, the following is not a legitimate statement and would result in a compile-time error:

40 = g

 

Top course recommendations for you

    Linux Tutorial
    2 hrs
    Beginner
    55.7K+ Learners
    4.47  (5602)
    Functions in Python
    1 hrs
    Beginner
    17.9K+ Learners
    4.46  (1136)
    GitHub Tutorial for Beginners
    2 hrs
    Beginner
    20.7K+ Learners
    4.44  (1479)
    Trees in Java
    2 hrs
    Beginner
    8.3K+ Learners
    4.54  (241)
    C for Beginners
    2 hrs
    Beginner
    155.3K+ Learners
    4.49  (16533)
    Binary Trees
    2 hrs
    Intermediate
    5.6K+ Learners
    4.6  (233)
    PowerPoint for Beginners
    2 hrs
    Beginner
    72.5K+ Learners
    4.53  (5234)
    UI / UX for Beginners
    1 hrs
    Beginner
    323.8K+ Learners
    4.53  (35413)
    Splunk Tutorial
    1 hrs
    Beginner
    4.2K+ Learners
    4.5  (330)
    Turbo C++
    1 hrs
    Beginner
    16.2K+ Learners
    4.41  (722)