Variableandconstants
Variableandconstants
Variable Declarations
Variable Initialisations
Constants
Enumeration
Scope of Variable
2
Variable
A variable is used to hold the value that can be used further in the programming.
A variable is a simple name used to store the value of a specific data type in computer memory.
In VB.NET, each variable has a particular data type that determines the size, range, and fixed
space in computer memory.
With the help of variable, we can perform several operations and manipulate data values in any
programming language.
3
Variable Declarations
The Dim statement is used for variable declaration and storage allocation for one or more
variables. The Dim statement is used at module, class, structure, procedure or block level. .
Each variable in the variable list has the following syntax and parts −
New − optional. It creates a new instance of the class when the Dim statement runs.
datatype − Required if Option Strict is On. It specifies the data type of the variable.
initializer − Optional if New is not specified. Expression that is evaluated and assigned to the
variable when it is created.
4
Variable Declarations Syntax
Syntax: Dim [Variable_Name] As [Defined Data Type]
Name Descriptions
Dim It is used to declare and allocate the space for one or more variables in memory.
As It is a keyword that allows you to define the data type in the declaration statement.
Data Type It defines a data type that allows variables to store data types such as Char, String, Integer,
Decimal, Long, etc.
5
Variable Declarations Example
Note:
If we want to declare more than one variable in the same line, we must separate each variable with a comma.
6
Variable Initialization
After the declaration of a variable, we must assign a value to the variable. The following syntax
describes the initialization of a variable:
Syntax: Variable_name = value
7
Getting Values from the User:
In VB.NET, the Console class provides the Readline() function in the System namespace.
It is used to take input from the user and assign a value to a variable
Imports System
Module User_Data
Dim name As String Sub Main()
name = Console.ReadLine() Dim num As Integer
Dim age As Double
Or name = Console.ReadLine
Dim name As String
Console.WriteLine("Enter your favourite number")
' Console.ReadLine or Console.ReadLine() takes value from the user
num = Console.ReadLine
Console.WriteLine(" Enter Your Good name")
Note: Console.Read() and
'Read string data from the user
Console.ReadKey() function is name = Console.ReadLine
used to read a single character Console.WriteLine(" Enter your Age")
from the user. age = Console.ReadLine
Console.WriteLine(" You have entered {0}", num)
Console.WriteLine(" You have entered {0}", name)
Console.WriteLine(" You have entered {0}", age)
Console.ReadKey()
End Sub
End Module
8
Lvalues & Rvalues
Lvalue: It is an lvalue expression that refers to a memory location for storing the address of a
variable. An lvalue is a variable that can appear to the left or right of the assignment operator to
hold values. Furthermore, in comparison to or swapping the variables' values, we can also
define the variable on both sides (left or right-side) of the assignment operator.
Dim num As Integer
Num = 5
Or
Dim num As Integer = 5
But when we write the following statement, it generates a compile-time error because it is not a
valid statement.
Dim x As Integer
10 = x
Rvalue: It is an rvalue expression that is used to store a value in some address of memory. An
rvalue can appear only on the right- hand side because it is a value of the variable that defines
on the right-hand side.
Dim college_name As String
college_name = “ISM" // rvalue define at right side of the assignment operator.
9
Constants in VB.NET
The name constant refers to a fixed value that cannot be changed during the execution of a program.
It is also known as literals.
These constants can be of any data type, such as Integer, Double, String, Decimal, Single, character, enum,
etc.
In VB.NET, const is a keyword that is used to declare a variable as constant. The Const statement can be
used with module, structure, procedure, form, and class.
Constname It defines the name of the constant variable to store the values.
As It is a keyword that allows you to define the data type in the declaration statement.
Data Type It defines a data type that allows variables to store data types such as Char, String,
Integer, Decimal, Long, etc.
10
Print and Display Constant
Const Name Descriptions
vbNullString Not the same as a zero-length string (""); used for calling external procedures.
vbObjectError Error number. User-defined error numbers should be greater than this value. For example:
Err.Raise(Number) = vbObjectError + 1000
11
Enumerations in VB.NET
An enumerated type is declared using the Enum statement.
The Enum statement declares an enumeration and defines the values of its members.
The Enum statement can be used at the module, class, structure, procedure, or block level.
Each member in the memberlist has the following syntax and Example:
parts: Enum Colors
red = 1
[< attribute list >] member name [ = initializer ]
Where, orange = 2
yellow = 3
name − specifies the name of the member. Required. green = 4
azure = 5
initializer − value assigned to the enumeration member. Optional. blue = 6
violet = 7
End Enum
12
Print and Display Constant
Const Name Descriptions
vbNullString Not the same as a zero-length string (""); used for calling external procedures.
vbObjectError Error number. User-defined error numbers should be greater than this value. For example:
Err.Raise(Number) = vbObjectError + 1000
13
Scope of Variables
The scope of a variable determines the accessible range of a defined variable at the time of
declaration in any block, module, and class.
It can be accessed, if the variable is in a particular region or scope in the same block. And if the
variable goes beyond the region, its scope expires.
The following are the methods to represent the scope of a variable in VB.NET.
1. Procedure Scope
2. Module Scope
3. Public Scope
14
Procedure Scope
A local variable is a type of variable defined within a procedure scope, block, or function. It is
available with a code inside the procedure, and it can be declared using the Dim or
static statement.
These variables are not accessible from outside of the local method. However, the local
variable can be easily accessed by the nested programming function in the same method.
Dim X As Integer
Local variables exist until the procedure in which they are declared is executed. Once a
procedure is executed, the values of its local variables will be lost, and the resources used by
these variables will be released. And when the block is executed again, all the local variables
are rearranged.
15
Module Scope
All existing procedures can easily identify a variable that is declared inside a module sheet is
called a module-level variable.
The defined module variable is visible to all procedures within that module only, but it is not
available for other module's procedures.
The Dim or private statement at the top of the first procedure declaration can be declared the
module-level variables. It means that these variables cannot be declared inside any procedure
block.
Further, these variables are useful to share information between the procedures in the same
module.
And one more thing about the module-level variable is that these variables can remains
existence as long as the module is executed.
16
Global Scope
As the name defines, a global variable is a variable that is used to access the
variables globally in a program.
It means these variables can be accessed by all the procedures or modules available in a
program.
To access the variables globally in a program, we need to use the friend or public
keyword with a variable in a module or class at the top of the first procedure function.
17