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
    42.3K+ Learners
    4.51  (2608)
    Functions in Python
    1 hrs
    Beginner
    14.5K+ Learners
    4.56  (654)
    GitHub Tutorial for Beginners
    2 hrs
    Beginner
    15.2K+ Learners
    4.0  (1)
    Trees in Java
    2 hrs
    Beginner
    7.1K+ Learners
    4.51  (146)
    C for Beginners
    2 hrs
    Beginner
    120.5K+ Learners
    4.51  (7678)
    Binary Trees
    2 hrs
    Intermediate
    4.9K+ Learners
    4.58  (159)
    PowerPoint for Beginners
    2 hrs
    Beginner
    57.4K+ Learners
    4.55  (2511)
    UI / UX for Beginners
    1 hrs
    Beginner
    228.7K+ Learners
    4.53  (17023)
    Splunk Tutorial
    1 hrs
    Beginner
    3.5K+ Learners
    4.53  (188)
    Turbo C++
    1 hrs
    Beginner
    13.6K+ Learners
    4.44  (494)