0% found this document useful (0 votes)
110 views17 pages

VB Note 7

I. Visual Basic variables are locations in memory that store values during code execution. Variables and constants both reserve memory locations but variables can change value while constants cannot. II. Variables are declared with a name and data type. Common variable types include integers, strings, booleans, and doubles. Values are assigned to variables after declaration. III. The Console class allows taking user input via ReadLine and assigning it to variables to store values entered by the user.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views17 pages

VB Note 7

I. Visual Basic variables are locations in memory that store values during code execution. Variables and constants both reserve memory locations but variables can change value while constants cannot. II. Variables are declared with a name and data type. Common variable types include integers, strings, booleans, and doubles. Values are assigned to variables after declaration. III. The Console class allows taking user input via ReadLine and assigning it to variables to store values entered by the user.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

VB Note 7 2022

Variables and Constants


A variable is a location in memory where a value can be stored during the execution of a Visual
Basic application. Visual Basic variables are assigned names by the programmer when they are
declared so that they can easily be referenced with in the application code.
These values can be declared as variable if the value assigned changed later in the code. If the
value assigned remains without changing it, then, it is a constant. Thus, both variables and
constants reserve memory locations. These locations are for the storage of values of various
types. The values include numbers, characters and strings. These memory locations are assigned
names. The names help to reference these memory locations in VB programming.

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.

VB.NET Variables Declaration

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.

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.

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  

VB.NET Variable Initialization

After the declaration of a variable value must be assign to it.

The following syntax describes the initialization of a variable:

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

A program to use different types of variable declaration and initialization in VB.NET.

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

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

Values from the User in VB .NET

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

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

Example

1. Dim num As Integer  
2. Num = 5   
3. Or   
VB.NET Constants

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:

1. Const constname As datatype = value  

Item Descriptions
Name

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

It defines the name of the constant variable to store the values.


Constname

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.

Value Assign a value to the variable as constant.

Example of Const keyword

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

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

The methods representing the scope of a variable in VB.NET.


1. Procedure or local Scope
2. Module Scope
3. Public 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.

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

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

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.

It is the declaration section of the module

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

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.

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

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

Data Types Required Space Value Range

Boolean A Boolean type depends on the True or False


implementing platform

Byte 1 byte Byte Range start from 0 to 255 (unsigned)

Char 2 bytes Char Range start from 0 to 65535 (unsigned)

Date 8 bytes Date range can be 0:00:0 (midnight) January 1, 0001 to 11:5959
PM of December 31, 9999.

Decimal 16 bytes Range from 0 to +/-79,228,162,514,264,337,593,543,950,335


(+/-7.9…E+28) without any decimal point;
And 0 to +/-7.92281625142264337593543950335 with 28 position
to the right of the decimal

Double 8 bytes -1.79769313486231570E+308 to -4.94-65645841246544E-324 for


negative values;
4.94065645841246544E-324 to 1.79769313486231570E+308, for
positive values

Integer 4 bytes -2,147,483,648 to 2,147,483,647 (signed)

Long 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (9.2…


E + 18) (signed)

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

SByte 1 byte -128 to 127 (signed)


Short 2 bytes -32,768 to 32,767 (signed)

Single 4 bytes -3.4028235E + 38 to -1.401298E-45 for negative values;


And for positive value: 1.401298E-45 to 3.4028235E + 38.

String String Datatype depend on the It accepts Unicode character from 0 to approximately 2 billion
implementing platform characters.

UInteger 4 bytes The range start from 0 to 4,294,967,295 (unsigned)

ULong 8 bytes The range of ULong start from 0 to 18,446,744,073,709,551,615


(1.8…E + 19) (unsigned)

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.

UShort 2 bytes Range from 0 to 65,535 (unsigned)

VB.NET Data Type

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

Welcome to the JavaTpoint


Byte is: 1
Integer number is: 20
Single data type is: 0.12
Double data type is: 2131.787
Today is: 31-05-2020 00:00:00
Character is: 1
String message is: Hello Friends...

You might also like