VB Note 7
VB Note 7
Example
Storing the interest rate for a banking application:
first, the variable will be declared
the variable will have a choosing name e.g., interestRate
this variable must be specified, e.g, Integer type, because the variable will be storing numbers
when variable is declared, a value is assigned to it
the value should be readable anywhere in your application code.
Types of Variables
I. Boolean variable
II. Byte variable
III. Char variable
IV. Decimal variable
V. Double variable
VI. Date variable
VII. Integer variable
VIII. Long variable
IX. Object variable
X. Short variable
XI. String variable
Boolean Variable
The Visual Basic Boolean variable type holds a value of true or false. These values are actually
stored as 1 and 0. 1 represent true while 0 represent false respectively. Boolean values are used
in conditional statements to decide whether particular parts of VB code should be executed or
not.
Byte Variable
A Byte variable holds a positive number in the range 0 to 255. The variables should be used with
caution because its range is limited. An attempt to assign a negative value or a value greater than
255 to a Visual Basic Byte variable will result in an error.
Char variable
Char variable type holds a single character. The value of a Char can be any character, e.g., D.
Internally, a number in the range 0 to 65,553 represents the character value. This is verifable in
ASCII table. When the letter 'Z' is assigned to a VB Char variable, the number 90 is actually
stored in the variable location.
Decimal variable
Decimal variable type can store either a whole number or a decimal number up to 29 decimal
places when working with whole numbers. It is recommended that the Integer variable type be
used as this is more memory efficient than the Decimal variable type.
Double variable
VB Double variable could accommodate small numbers or very large numbers which requires
more than 28 decimal places. VB Double variables can store positive numbers in their range.
Double variables are effective for creating scientific applications.
Date variable
This is a variable type that holds a date and time values. Dates are declared in the form
#mm/dd/yyyy#. March, 10, 2022 would be declared as #3/10/2022#. Also, VB provides a
number of mechanisms for working with Date variables. An example of such is a mechanism
provided to calculate a date which is eight months from today's date.
Integer variable
VB Integer variable type is implemented in storing whole numbers. It does not store decimal
values.
Long variable
VB Long variable type is for storing whole numbers ranging.
Example
-9,233,372,036,854,775,808 to 9,233,372,036,854,775,807
Object variable
The Object variable is a catch-all variable type. Its flexibility allows storing a value of any data
type. Basically, an Object variable reserves more memory than most variable types. Hence, it is
advisable to use the correct variable type for the value storing rather than use the Object variable
type.
Short variable
A Short variable in VB stores a whole number in the range -32,768 to 32,767.
String variable
String variable type stores multiple characters that make up words or sentences. String variables
are encapsulated in double-quotation marks (").
Example "Hello" is a string, as is "Hello, this is a test".
VB provides a number of mechanisms for manipulating strings.
Variables are declared using the Visual Basic Dim keyword.
The declaration of a variable requires a variable name and data type which could be followed by
a Dim. Note that a Dim is used in Class, Module, structure, Sub, procedure.
Syntax:
1. 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.
Note
If we want to declare more than one variable in the same line, we must separate each
variable with a comma.
Below are some declarations of variables with the definition of their data type
1. Dim Roll_no As Integer
2. Dim Emp_name As String
3. Dim Salary As Double
4. Dim Emp_id, Stud_id As Integer
5. Dim result_status As Boolean
Syntax
Syntax is to the rules that define the structure of a language. Also, Syntax in computer
programming means the rules that control the structure of the words, symbols and punctuation of
a programming language. It will be impossible to understand a programming language structure
without the syntax.
Dim Variable_name1 As DataType1, variable_name2 As DataType2, Variable_name3 ADataType3
These statements are used to declare the variable with their data type
1. Static name As String
2. Public bill As Decimal = 0
Syntax:
1. Variable_name = value
For example:
1. Dim Roll_no As Integer 'declaration of Roll_no
2. Roll_no = 101 'initialization of Roll_no
3. Initialize the Emp_name
4. Dim Emp_name As String
5. Emp_name = "David" 'Here Emp_name variable assigned a value of David
6. Initialize a Boolean variable
7. Dim status As Boolean 'Boolean value can be True or False.
8. status = True 'Initialize status value to True
Variable can be initialized at the time of declaration
1. Dim Roll_no As Integer = 101
2. Dim Emp_name As String = " Stephen Robert "
Example
Variable1.vb
Input
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
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.
Example
1. Dim name As String
2. name = Console.ReadLine()
3. Or name = Console.ReadLine
User_Data.vb
Input
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
Example
1. Dim num As Integer
2. Num = 5
3. Or
VB.NET Constants
Declaration of Constants
Syntax:
1. Const constname As datatype = value
Item Descriptions
Name
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, Intege
Decimal, Long, etc.
Const1.vb
Input
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
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. If the variable goes beyond the region, its scope expires.
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.
1. 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.
Example
Local_Scope.vb
Input
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
Module Scope
When all existing procedures can easily identify a variable that is declared inside a module sheet
is known as a module-level variable. The module variable is visible to all procedures within that
module. It is not available for other module 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. Also, this module-level
variable continue to exist as long as the module is executed.
1. Private num As Integer ' A private module-level variable
2. Dim name As String ' Another private module-level variable
Example
Module_scope.vb
Input
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
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.
Example
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
Date 8 bytes Date range can be 0:00:0 (midnight) January 1, 0001 to 11:5959
PM of December 31, 9999.
Object Object size based on the platform such as 4 It can store any type of data defined in a variable of type Object
bytes in 32-bit and 8 bytes in 64-bit
platform
String String Datatype depend on the It accepts Unicode character from 0 to approximately 2 billion
implementing platform characters.
User-Defined A user-defined data type depends on the Each member of the structure has its own data type and limits
(structure) implementing platform independent of the other members' ranges.
VB.NET data type is used to define the type of a variable or function in a program. Also, the
conversion of one data type to another type using the data conversion function.
A Data Type refers to which type of data or value is assigning to a variable or function. This
will enable a variable to hold a defined data type value.
Example
When a variable is declared, computer need to know the type of data or value is allocated. There
are different kinds of variables to hold different amounts of space in computer memory.
Syntax:
1. Dim Variable_Name as DataType
VariableName: It defines the name of the variable that you assign to store values.
DataType: It represents the name of the data type that you assign to a variable.
Different Data Types and their allocating spaces in VB.NET
The following table shows the various data types list in the VB.NET programming language.
Example
Data_type.vb
Module Data_type
Sub Main()
' defining the Data Type to the variables
Dim b As Byte = 1
Dim num As Integer = 5
Dim si As Single
Dim db As Double
Dim get_date As Date
Dim c As Char
Dim str As String
b = 1
num = 20
si = 0.12
db = 2131.787
get_date = Today
c = "A"
str = "Hello Friends..."
Console.WriteLine("Welcome to the JavaTpoint")
Console.WriteLine("Byte is: {0}", b)
Console.WriteLine("Integer number is: {0}", num)
Console.WriteLine("Single data type is: {0}", si)
Console.WriteLine("Double data type is: {0}", db)
Console.WriteLine("Today is: {0}", get_date)
Console.WriteLine("Character is: {0}", b)
Console.WriteLine("String message is: {0}", str)
Console.ReadKey()
End Sub
End Module
Output