KEMBAR78
Variable and Constant - Javatpoint | PDF | Variable (Computer Science) | Data Type
0% found this document useful (0 votes)
29 views9 pages

Variable and Constant - Javatpoint

The document provides an overview of variables and constants in VB.NET, detailing how to declare, initialize, and use them within programs. It explains the concepts of lvalues and rvalues, as well as the scope of variables, including procedure, module, and global scopes. Additionally, it includes examples and syntax for variable and constant declarations, initialization, and user input handling.

Uploaded by

PHẠM HỮU ÁI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views9 pages

Variable and Constant - Javatpoint

The document provides an overview of variables and constants in VB.NET, detailing how to declare, initialize, and use them within programs. It explains the concepts of lvalues and rvalues, as well as the scope of variables, including procedure, module, and global scopes. Additionally, it includes examples and syntax for variable and constant declarations, initialization, and user input handling.

Uploaded by

PHẠM HỮU ÁI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

VB.

NET Variable and Constant


In VB.NET, a variable is used to hold the value that can be used further in the programming. In this section, we will learn how to declare
and initialize a variable and a constant.

What is a Variable?
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.

VB.NET Variables Declaration


The declaration of a variable is simple that requires a variable name and data type followed by a Dim. A Dim is used in Class, Module,
structure, Sub, procedure.

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.

Variable_Name It defines the name of the 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.

Value Assign a value to the variable.

There are some valid declarations of variables along with their data type definition, as shown below:

Dim Roll_no As Integer


Dim Emp_name As String
Dim Salary As Double
Dim Emp_id, Stud_id As Integer
Dim result_status As Boolean

Further, if we want to declare more than one variable in the same line, we must separate each variable with a comma.

Syntax

Dim Variable_name1 As DataType1, variable_name2 As DataType2, Variable_name3 As DataType3


Note: The statements given below is also used to declare the variable with their data type:

Static name As String


Public bill As Decimal = 0

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

For example:

Advertisement

Dim Roll_no As Integer 'declaration of Roll_no


Roll_no = 101 'initialization of Roll_no

Initialize the Emp_name


Dim Emp_name As String
Emp_name = "David" 'Here Emp_name variable assigned a value of David

Initialize a Boolean variable


Dim status As Boolean 'Boolean value can be True or False.
status = True 'Initialize status value to True

We can also initialize a variable at the time of declaration:

Dim Roll_no As Integer = 101


Dim Emp_name As String = " Stephen Robert "

Let's create a program to use different types of variable declaration and initialization in VB.NET.

Variable1.vb

Imports System
Module Variable1
Sub Main()
'declaration of intData as Integer
Dim intData As Integer
'declaration of charData as Char
Dim CharData As Char
'declaration of strData as String
Dim strData As String
'declaration of dblData as Double
Dim dblData As Double
'declaration of single_data as Single
Dim single_data As Single
'Initialization of intData
intData = 10
'Initialization of CharData
CharData = "A"
'Initialization of strData
strData = " VB.NET is a Programming Language."
dblData = 4567.676
'Initialization of dblData
'Initialization of single_data
single_data = 23.08

Console.WriteLine(" Value of intData is: {0}", intData)


Console.WriteLine(" Value of CharData is: {0}", CharData)
Console.WriteLine(" Value of strData is: {0}", strData)
Console.WriteLine(" Value of dblData is: {0}", dblData)
Console.WriteLine(" Value of single_data is: {0}", single_data)

Console.WriteLine("press any key to exit...")


Console.ReadKey()
End Sub

End Module

Output:

Value of intData is: 10


Value of CharData is: A
Value of strData is: VB.NET is a Programming Language.
Value of dblData is: 4567.676
Value of single_data is: 23.08
press any key to exit...

Getting Values from the User in VB.NET


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. For example:

Dim name As String


name = Console.ReadLine()
Or name = Console.ReadLine

Let's create a program that takes input from the user.

User_Data.vb

Imports System
Module User_Data
Sub Main()
Dim num As Integer
Dim age As Double
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")
'Read string data from the user
name = Console.ReadLine
Console.WriteLine(" Enter your Age")
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

Output:

Enter your favourite number


7
Enter Your Good name
Alexander
Enter your Age
27.5
You have entered 7
You have entered Alexander
You have entered 27.5

Note: Console.Read and Console.ReadKey() function is used to read a single character from the user.

Lvalues and Rvalues in VB.NET


There are two ways to express the expression value:

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.

Example:

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 name As String


Name = "Peter" // rvalue define at right side of the assignment operator.

VB.NET Constants
As the name suggests, 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.

Declaration of Constants
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.

Syntax:

Const constname As datatype = value

Item Name Descriptions

Const It is a Const keyword to declare a variable as constant.

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.

Value Assign a value to the variable as constant.

Further, if we want to declare more than one variable in the same line, we must separate each variable with a comma, as shown below.
The Syntax for defining the multiple variables as constant is:

Dim Variable_name1 As DataType1, variable_name2 As DataType2, Variable_name3 As DataType3

Note: The statements given below are also used to declare the variable with their data type:

Const num As Integer = 10


Static name As String
Public Const name As String = "JavaTpoint"
Private Const PI As Double = 3.14

Example of Const keyword

Advertisement

Const1.vb

Module Const1
Sub main()
'declaration and initialization of Constant variable using Const keywords
Const intData As Integer = 20
Const name As String = "JavaTpoint"
Const topic As String = "VB.NET"
Const PI = 3.14
Dim radius, area As Integer

Console.WriteLine(" Constant integer is {0}", intData)


Console.WriteLine(" You have entered {0}", name)
Console.WriteLine(" Your Topic is {0}", topic)
Console.WriteLine("Enter the Radius")
radius = Console.ReadLine()
area = PI * radius * radius
Console.WriteLine(" Area of Circle is {0}", area)
Console.ReadKey()

End Sub
End Module

Output:

Constant integer is 20
You have entered JavaTpoint
Your Topic is VB.NET
Enter the Radius
7
Area of Circle is 154

Scope of Variable in VB.NET


The scope of a variable determines the accessible range of a defined variable at the time of declaration in any block, module, and class. You
can access it 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

Procedure (local) 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.

Let's create a program that displays the local scope of a variable within a function.

Local_Scope.vb

Imports System
Module Local_scope
Sub Main()
Console.WriteLine(" Scope of local varibale within a function")
local() ' call local() and local() function without any object reference
local2()
Console.WriteLine("press any key to exit...")
Console.ReadKey()
End Sub
Sub local()
Dim X As Integer
' declaration of local variable
X = 50
Console.WriteLine(" Value of Local value X is {0}", X)

End Sub
Sub local2()
Dim X As String
' scope of local variable within a function
X = "JavaTpoint"
Console.WriteLine(" Value of X is {0}", X)
End Sub
End Module

Output:

Scope of local variable within a function


Value of Local value X is 50
Value of X is JavaTpoint
press any key to exit...

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.

' It is the declaration section of the module


Private num As Integer ' A private module-level variable
Dim name As String ' Another private module-level variable

Let's create a program that display the module level variable in VB.NET.

Module_scope.vb

Imports System
Module Module_scope
'module-level variable declaration
Dim x As Integer
Private y As Integer
Private name As String = "JavaTpoint"
Sub example()
x = 10
y = x + 10
Console.WriteLine(" Value of Y is {0}", y)
End Sub
Sub example2()
Console.WriteLine(" Value of X is {0}", x)
Console.WriteLine(" Value of Y is {0}", y)
Console.WriteLine(" Name is {0}", name)
End Sub
Sub example3()
Dim A As Integer ' local variable or local scope
A=x+y
Console.WriteLine(" Local scope within a function of variable A {0}", A)
End Sub
Sub Main()
Console.WriteLine(" Module scope of variable")
example()
example2()
example3()
Console.WriteLine("Press any key to exit...")
Console.ReadKey()
End Sub
End Module

Output:

Module scope of variable


Value of Y is 20
Value of X is 10
Value of Y is 20
Name is JavaTpoint
Local scope within a function of variable A 30
Press any key to exit...

Global (Public) 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, you need to use the
friend or public keyword with a variable in a module or class at the top of the first procedure function. Global scope is also known as
the Namespace scope.

Let's create a program that uses the global variable.

Global_scope1.vb

Imports System
Module Global_scope1
'Global declaration of a variable
Public str As String = "Hello, Programmer."
Public topic As String
Public exp As Integer

Sub Main()
Console.WriteLine(" You have passed {0}", str)
Console.WriteLine(" Enter the topic name")
topic = Console.ReadLine
Console.WriteLine(" Topic Name :{0}", topic)
Console.WriteLine("How many years of experienced in {0}?", topic)
exp = Console.ReadLine
Console.WriteLine(" Your Experienced is {0} ", exp)
Console.ReadKey()
End Sub

End Module

Output:

You have passed Hello, Programmer


Enter the topic name
VB.NET
Topic Name :VB.NET
How many years of experienced in VB.NET?
10
Your Experienced is 10

You might also like