Docsity

Visual Basic Programming Language: A Comprehensive Guide - Prof. Ah, Lecture notes of Computer Science

Toggle sidebar
bg1
PROGRAM
Computer is a machine so computer, do nothing its self we must give some instruction to perform any task.
The set of instruction that is given to computer to solve any problem is called program.
LANGUAGE
When we have to solve any problem then we write computer program in which we give some instruction to
computer. These instructions are written in some special computer language. These languages are divided in
to three general types.
Machine language
Assembly Language
High Level Language
Any computer directly understands only its own machine language. Machine language is natural language of
computer. Machine language composed on binary code (0 and 1). Machine language is machine dependent
language. It means that particular machine language only run on one type of computer. It is difficult to
understand the machine language so with passing of time and development assembly language was
introduced which consist on some abbreviations of machine code. User directly writes these abbreviations, a
translator program converts these assembly codes in to machine codes. This translator, which translates the
assembly codes in to machine codes, is called ASSEMBLER.
The modern computer languages are High-level languages. In these types of languages, we write instruction
in simple English language. We also use simple mathematical notation like that mathematical operators and
expressions. A translator program converts the program written in high level language into machine code is
called compiler and interpreter.
HISTORY OF VISUAL BASIC
In History of Programming there are tons of programming languages were introduced and used by
worldwide. But one particular programming language change everything in computer programming which is
Visual Basic because It is totally stand alone when you compare with normal programming languages. Once
upon a time GUI Applications are the most critical one in computing. It also tough one to develop. Because
everything done through codes.
Before Visual Basic, Windows components are developed by C++. It contains many bugs so it reduces the
performance of windows. So, Microsoft decided to develop a new programming language which makes
everything simple and efficient one. Finally, Visual Basic was born and it make many revolutionary changes
in windows.
In 1991 History was re-written GUI Application development becomes easy. Yes! Microsoft introduced
Visual Basic so the most powerful programming language was born. Visual Basic helps everyone to develop
GUI applications by just drag-drop so you don’t need to write hard code to create the user interface you can
create everything by drag and drop properties.
Visual Basic is a programming which allows developer to develop Graphical User Interface by using Drag
and Drop properties. It is developed by Alan Cooper. He uses traditional BASIC Programming language
concepts to develop Visual Basic. With help of Visual Basic IDE a Programmer can develop a simple to
Complex GUI Programs.
Till now there are many Versions of Visual Basic are introduced. First version Visual Basic 1.0 was
introduced in May 1991.Most Successful version of Visual Basic 6.0 were introduced in mid of 1998. It
supports web and desktop application development. Later it entered Microsoft’s “Non-Supported phase”
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22

Partial preview of the text

Download Visual Basic Programming Language: A Comprehensive Guide - Prof. Ah and more Lecture notes Computer Science in PDF only on Docsity!

PROGRAM

Computer is a machine so computer, do nothing its self we must give some instruction to perform any task.

The set of instruction that is given to computer to solve any problem is called program.

LANGUAGE

When we have to solve any problem then we write computer program in which we give some instruction to

computer. These instructions are written in some special computer language. These languages are divided in

to three general types.

 Machine language  Assembly Language  High Level Language

Any computer directly understands only its own machine language. Machine language is natural language of

computer. Machine language composed on binary code (0 and 1). Machine language is machine dependent

language. It means that particular machine language only run on one type of computer. It is difficult to

understand the machine language so with passing of time and development assembly language was

introduced which consist on some abbreviations of machine code. User directly writes these abbreviations, a

translator program converts these assembly codes in to machine codes. This translator, which translates the

assembly codes in to machine codes, is called ASSEMBLER.

The modern computer languages are High-level languages. In these types of languages, we write instruction

in simple English language. We also use simple mathematical notation like that mathematical operators and

expressions. A translator program converts the program written in high level language into machine code is

called compiler and interpreter.

HISTORY OF VISUAL BASIC

In History of Programming there are tons of programming languages were introduced and used by

worldwide. But one particular programming language change everything in computer programming which is

Visual Basic because It is totally stand alone when you compare with normal programming languages. Once

upon a time GUI Applications are the most critical one in computing. It also tough one to develop. Because

everything done through codes.

Before Visual Basic, Windows components are developed by C++. It contains many bugs so it reduces the

performance of windows. So, Microsoft decided to develop a new programming language which makes

everything simple and efficient one. Finally, Visual Basic was born and it make many revolutionary changes

in windows.

In 1991 History was re-written GUI Application development becomes easy. Yes! Microsoft introduced

Visual Basic so the most powerful programming language was born. Visual Basic helps everyone to develop

GUI applications by just drag-drop so you don’t need to write hard code to create the user interface you can

create everything by drag and drop properties.

Visual Basic is a programming which allows developer to develop Graphical User Interface by using Drag

and Drop properties. It is developed by Alan Cooper. He uses traditional BASIC Programming language

concepts to develop Visual Basic. With help of Visual Basic IDE a Programmer can develop a simple to

Complex GUI Programs.

Till now there are many Versions of Visual Basic are introduced. First version Visual Basic 1.0 was

introduced in May 1991.Most Successful version of Visual Basic 6.0 were introduced in mid of 1998. It

supports web and desktop application development. Later it entered Microsoft’s “Non-Supported phase”

because they introduced Visual Basic .net. It is nothing but a successor of Visual Basic 6.0. Some call it as

Visual Basic 7.0. But Runtime of Visual Basic supported in Windows XP, Vista, Windows Server 2008 and

Windows 7.

WHAT IS VISUAL BASIC

Visual Basic is an event-driven Programming Language which is used to develop Simple to Complex GUI

Applications using drag and drop components of Visual Basic IDE. We can call it as “Development

Environment” too Because It has Program development as well as executing capability. Visual Basic was

developed by Alan Cooper using BASIC Programming Language. Syntax of Visual Basic is completely

different when you compare with other famous programming languages such as C, C++, Java and C #.

Visual Basic is different from other programming languages because of its simplicity and powerful nature.

That combination makes the Visual Basic programming language great to use. Visual Basic is not a true

Object-Oriented Programming Language. It may call as event-driven language because each object can react

to different events such as mouse click, text change, mouse over, got focus.

In Visual Basic we don’t need to worry about the syntax because Programmer can easily drag and drop the

components (Buttons, Check box, Text box etc.) over the form (Form is nothing but a Container which is

used to place different set of controls).

WHAT IS VB.NET?

VB.NET stands for Visual Basic.NET, and it is a computer programming language developed by Microsoft.

It was first released in 2002 to replace Visual Basic 6. VB.NET is an object-oriented programming

language. This means that it supports the features of object-oriented programming which include

encapsulation, polymorphism, abstraction, and inheritance.

Visual Basic .ASP NET runs on the .NET framework, which means that it has full access to the .NET

libraries. It is a very productive tool for rapid creation of a wide range of Web, Windows, Office, and

Mobile applications that have been built on the .NET framework.

The language was designed in such a way that it is easy to understand to both novice and advanced

programmers. Since VB.NET relies on the .NET framework, programs written in the language run with

much reliability and scalability. With VB.NET, you can create applications that are fully object-oriented,

similar to the ones created in other languages like C++, Java, or C#. Programs written in VB.NET can also

interoperate well with programs written in Visual C++, Visual C#, and Visual J#. VB.NET treats everything

as an object. It is true that VB.NET is an evolved version of Visual Basic 6, but it’s not compatible with it. If

you write your code in Visual Basic 6, you cannot compile it under VB.NET.

SOME VB.NET FEATURES

VB.NET comes loaded with numerous features that have made it a popular programming language amongst

programmers worldwide. These features include the following:

 VB.NET is not case sensitive like other languages such as C++ and Java.  It is an object-oriented programming language. It treats everything as an object.  Automatic code formatting, XML designer, improved object browser etc.  Support for Boolean conditions for decision making.  Simple generics.  A standard library.  Events management.  Attributes, which are tags for providing additional information regarding elements that have been defined within a program.  Windows Forms- you can inherit your form from an already existing form.

Once you click on Project , a new popup will open in that select Visual Basic from the left pane and choose Console App or Windows form App. In the Name section, give any name for your project and select an appropriate Location path to save your project files, and click OK like as shown below.

Once we click on the OK button, a new console application will be created like as shown below:

This is how we can create a console application in a visual basic programming language using a visual studio based on our requirements.

VISUAL BASIC HELLO WORLD PROGRAM EXAMPLE

Now, replace your Module1.vb file code like as shown following to display the “ Hello World ” message.

Imports System Module Module Sub Main() Console.WriteLine("Hello World!") Console.WriteLine("Press Enter Key to Exit.") Console.ReadLine() End Sub End Module

If you observe the above code, we used many parameters to implement the “ Hello World ” program in

visual basic. In the next section, we will learn all the parameters in detail.

Visual Basic Hello World Program Structure

Now we will go through each step of our visual basic program and learn each parameter in a detailed

manner.

Imports System;

Here, Imports System is the .NET Framework library namespaces and we used Imports keyword to

import system namespace to use existing class methods such WriteLine(), ReadLine(), etc. By default,

the .NET Framework provides a lot of namespaces to make the application implementation easy.

The namespace is a collection of classes, and classes are the collection of objects and methods.

Module Module

Here, Module Module1 is used to define a module ( Module1 ). The module ( Module1 ) will contain all the

variables, methods, etc., based on our requirements.

Sub Main()

Here, Sub Main() is used to define a method in our module ( Module1 ).

The keyword Sub is a procedure and it is helpful to write a series of Visual Basic statements

within Sub and End Sub statements.

The name Main will refer to the name of our method in module ( Module1 ). The Main() method is the entry

point of our console application.

Console.WriteLine() / ReadLine()

Here, Console.WriteLine() and Console.ReadLine() methods are used to write a text to the console and read

the input from the console.

The Console is a class of .NET Framework namespace System and WriteLine() and ReadLine() are the

methods of Console class.

Compile & Run VB Hello World Program

To see the output of our Visual Basic Hello World Program, we need to compile and run the application by

pressing either Ctrl + F5 or click on the Start option in the menu bar like as shown below.

Once we click on the Start option or Ctrl + F5, our program will compile and show the result below.

This is how we can create and execute the applications in visual basic (vb) programming language using visual studio based on our requirements. VISUAL BASIC VARIABLES AND DATA TYPES

In Visual Basic, Variables will represent storage locations, and each variable will have a particular data

type to determine the type of values the variable can hold.

Data Type Size Range

Single 4 bytes -3.4028235E+38 through -1.401298E-45 † for negative values; 1.401298E- through 3.4028235E+38 † for positive values

String Depends on Platform 0 to approximately 2 billion Unicode characters

UInteger 4 bytes 0 to 4,294,967,

ULong 8 bytes 0 to 18,446,744,073,709,551,615 (1.8...E+19 †)

UShort 2 bytes 0 to 65,

User- Defined

Depends on Platform Each member of the structure has a range determined by its data type and is independent of the ranges of the other members

Now, we will see how to use the Data Types in our visual basic applications with examples.

Visual Basic Data Types Example

Following is the example of using the data types in visual basic.

Module Module Sub Main() Dim id As Integer Dim name As String = "Musa Ibrahim" Dim percentage As Double = 10. Dim gender As Char = "M"c Dim isVerified As Boolean id = 10 isVerified = True Console.WriteLine("Id:{0}", id) Console.WriteLine("Name:{0}", name) Console.WriteLine("Percentage:{0}", percentage) Console.WriteLine("Gender:{0}", gender) Console.WriteLine("Verfied:{0}", isVerified) Console.ReadLine() End Sub End Module

The above example shows that we defined multiple variables with different data types and assigned the values based on our requirements. When we execute the above example, we will get the result as shown below.

Id: Name:Musa Ibrahim Percentage:10. Gender:M Verfied:True

This is how we can use the data types in Visual Basic applications based on our requirements.

Visual Basic Comments

In Visual Basic, Comments are the self-explanatory notes to provide detailed information about the code which we wrote in our applications.

By using comment symbol ('), we can comment on the code in our Visual Basic programming. The comment symbol (') will tell the Visual Basic compiler to ignore the text following it, or the comment.

It’s always a good practice to include the comments in your Visual Basic code to provide detailed information about the functionality like what the specific block or line of code can do and it will be a benefit for anyone else who examines the code.

In Visual Basic, we can include the comments anywhere in the program without effecting our code and the comments in Visual Basic do not affect the performance of an application because the comments won’t be compiled and executed by the compiler.

Following is the example of defining the comments in Visual Basic programming language.

' Show Greet Messaging Console.WriteLine("Welcome to Computer department ") Console.ReadLine() ' This method to read the commands from a console In case, if you want to comment more than one line, use the comment symbol (') on each line like as shown below.

' Show Greet Messaging ' Multiple line comments Visual Basic Comments Example

Following is the example of defining the comments in Visual Basic programming language.

Module Module Sub Main() ' Calling Method to Show Greet Messaging GreetMessage() Console.WriteLine("Press Any Key to Exit..") Console.ReadLine() ' This method to read the commands from a console End Sub ' This Method will display the welcome message Public Sub GreetMessage() Console.WriteLine("Welcome to CHST") End Sub End Module

If you observe the above example, we defined multiple comments by using comment symbol (') to provide information about functionality. When we execute the above Visual Basic program, we will get the result as shown below.

Welcome to CHST Press Any Key to Exit..

This is how we can use the comments to provide detailed information about the functionality of our Visual

Basic application.

In visual studio, we can use the following keyboard shortcuts to comment/uncomment the selected lines of

code based on our requirements.

To comment keyboard shortcut is Ctrl + K, C and to uncomment keyboard shortcut is Ctrl + K, U.

VISUAL BASIC OPERATORS

In Visual Basic, Operator is a programming element that specifies what operation needs to perform on operands or variables. For example, an addition (+) operator in Visual Basic is used to perform the sum

operation on operands.

Visual Basic Operator Types

In Visual Basic different types of operators are available; those are

 Arithmetic Operators  Assignment Operators

Operator Description Example

<<= It will move the left operand bit values to the left based on the number of positions specified by the second operand.

a <<= 2 equals to a = a << 2

Visual Basic Logical / Bitwise Operators

In Visual Basic, Logical / Bitwise Operators are useful to perform the logical operation between two

operands like AND, OR, etc., based on our requirements. The Logical / Bitwise Operators will always work

with Boolean expressions ( true or false ) and return Boolean values.

The following table lists the different types of logical/bitwise operators available in Visual Basic.

Operator Description

Example (a = True, b = False)

And It will return true if both operands are non zero. a And b = False

Or It will return true if any one operand becomes a non zero. a Or b = True

Not It will return the reverse of a logical state that means if both operands are non zero, it will return false.

Not(a And b) = True

Xor It will return true if any one of expression1 and expression2 evaluates to true. a Xor b = True

AndAlso It will perform the short-circuiting logical operation and return true if both operands evaluate to true.

a AndAlso b = False

OrElse It will perform the short-circuiting logical operation and return true if any operand evaluates to true.

a OrElse b = True

IsFalse It will determine whether an expression is False.

IsTrue It will determine whether an expression is True.

Visual Basic Comparison Operators

In Visual Basic, Comparison Operators are useful to determine whether the defined two operands are

equal, greater than or less than, etc., based on our requirements.

The following table lists the different comparison operators available in Visual Basic.

Operator Description Example (a = 10, b = 5)

< It will return true if the right operand is greater than the left operand. a < b = False

<= It will return true if the right operand is greater than or equal to the left operand. a <= b = False

It will return true if the left operand is greater than the right operand. a > b = True

= It will return true if the left operand is greater than or equal to the right operand. a >= b = True

= It will return true if both operands are equal. a = b = False

<> It will return true if both operands are not equal. a <> b = True

Operator Description Example (a = 10, b = 5)

Is It will return true if two object references refer to the same object.

IsNot It will return true if two object references refer to different objects.

Visual Basic Concatenation Operators

In Visual Basic, Concatenation Operators are useful to concatenate defined operands based on our

requirements.

The following table lists the different types of concatenation operators available in Visual Basic.

Operator Description Example (a = Hello, b = World)

& It will concatenate given two expressions. a & b = HelloWorld

  • It is useful to add two numbers or concatenate two string expressions. a + b = HelloWorld

VB.NET Control Statements In VB.NET , the control statements are the statements that controls the execution of the program on the basis of the specified condition. It is useful for determining whether a condition is true or not. If the condition is true, a single or block of statement is executed. The are control statements in visual basic:  IF Statements and  Loop Control statements

If statements: In Visual Basic, if statement is useful to execute the block of code or statements

conditionally based on the value of an expression. Generally, in Visual Basic, the statement that needs to be

executed based on the condition is known as a “ Conditional Statement ” and the statement is more likely a block of code.

Syntax of Visual Basic if Statement

Following is the syntax of defining the if statement in Visual Basic programming language.

If bool_expression Then // Statements to Execute if condition is true End If

If you observe the above If statement syntax, the statements inside of the If condition will be executed only when the “ bool_expression ” returns true otherwise, those statements will be ignored for execution. Following is the sample example of using the If statement in Visual Basic programming language. Dim x As Integer = 20 If x >= 10 Then Console.WriteLine("Number Greater than 10") End If

If we observe the above example, the Console statement will be executed only when the defined condition ( x

>= 10 ) returns true.

Visual Basic If Statement Flow Chart Diagram

Following is the flow chart diagram, which will represent the process flow of the If statement in Visual

Basic programming language.

the Boolean expression returns true , the If statements will be executed; otherwise, the Else block of statements will be executed. Syntax of Visual Basic If Else Statement

Following is the syntax of defining the If Else statement in Visual Basic programming language.

If boolean_expression Then // Statements to Execute if Boolean expression is True Else // Statements to Execute if Boolean expression is False End If

If you observe the above If Else statement syntax, the statements inside the If condition will be executed only when the “ bool_expression ” returns true otherwise, the statements inside of Else condition will be executed.

Following is the sample example of using the If Else statement in a visual basic programming language.

Dim x As Integer = 20 If x >= 10 Then Console.WriteLine("x is Greater than or equals 10") Else Console.WriteLine("x is less than or equals to 10") End If

If you observe the above example, whenever the defined condition ( x >= 10 ) returns true , the If condition

will be executed; otherwise, the Else block of code will be executed.

Visual Basic If Else Statement Flow Chart

Following is the flow chart diagram, representing the process flow of the If Else statement in Visual Basic

programming language.

If you observe the above Visual Basic If Else statement flow chart diagram when the defined condition

is true , the statements within the If condition will be executed; otherwise, the statements within

the Else block will be executed.

Visual Basic If Else Statement Example

Following is the example of defining the If Else statement in Visual Basic programming language to execute

the block of code or statements based on a Boolean expression.

Module Module Sub Main() Dim x As Integer = 20 If x >= 10 Then Console.WriteLine("x is Greater than or Equals to 10") Else Console.WriteLine("x is Less than 10") End If Console.WriteLine("Press Enter Key to Exit..") Console.ReadLine() End Sub End Module

If you observe the above example, we defined the If-Else condition to execute the statements based on the defined condition status i.e. either true or false. When we execute the above Visual Basic program, we will get the result as shown below.

If you observe the above result, the Boolean expression returns true that’s the reason only If block of statements has been executed and printed the statements in the console window.

This is how we can use the If Else statement in Visual Basic programming language to execute the block of code or statements based on our requirements. Visual Basic If-Else-If Statement

In Visual Basic, the If-Else-If statement or condition is useful for defining the multiple conditions and

executing only the matched condition based on our requirements.

Generally, Visual Basic, if statement or if-else statement is useful when we have one condition to validate

and execute the required block of statements. If we have multiple conditions to validate and execute only

one block of code, then the If-Else-If statement is useful in our application.

Syntax of Visual Basic If-Else-If Statement

Following is the syntax of defining the If Else If statement in Visual Basic programming language.

If condition_1 Then // Statements to Execute if condition_1 is True ElseIf condition_2 Then // Statements to Execute if condition_2 is True ElseIf condition_2 Then // Statements to Execute if condition_3 is True .... .... Else // Statements to Execute if all conditions are False End If

If you observe the above Visual Basic If-Else-If statement syntax, we defined multiple conditions to execute required statements.

Module Module Sub Main() Dim x As Integer = 5 If x = 10 Then Console.WriteLine("x value equals to 10") ElseIf x > 10 Then Console.WriteLine("x value greater than 10") Else Console.WriteLine("x value less than 10") End If Console.WriteLine("Press Enter Key to Exit..") Console.ReadLine() End Sub End Module

The above example shows that we defined the If-Else-If conditions to execute the statements based on defined condition status.

When we execute the above Visual Basic program, we will get the result as shown below.

If you observe the above result, all the defined conditions are failing due to that it executed the Else block of statements and printed the required statement in the console window.

This is how we can use the If-Else-If statement in Visual Basic programming language to execute the block of code or statements based on our requirements.

Visual Basic Nested If Else Statements

In Visual Basic, Nested If-Else statements are useful to include one if…else statement within another if… else statement to test one condition followed by another condition. Generally, in Visual Basic, placing one if…else statement within another if…else statement is called a nested if…else statement. Syntax of Visual Basic Nested If-Else Statement

Following is the syntax of defining the nested if…else statement in a visual basic programming language.

If condition Then If nested_condition_1 Then // Statements to Execute Else // Statements to Execute End If Else If nested_condition_2 Then // Statements to Execute Else // Statements to Execute End If End If

If you observe the above Visual Basic nested if-else statement syntax, we defined one if…else

statement within another if…else condition to perform one condition followed by another condition.

In the nested If-Else statement, the defined If condition returns true, then it will enter into the body of

condition and perform another if…else condition checking based on our requirements.

Visual Basic Nested If-Else Statement Flow Chart

Following is the flow chart diagram, representing the process flow of the nested-if-else statement in a visual

basic programming language.

If you observe the above Visual Basic nested if-else statements flow chart, when the defined condition is

true, then another if…else condition execution will happen and perform the required operations. Same way,

other nested if…else statements also will be executed based on our requirements.

Visual Basic Nested If-Else Statement Example

Following is the example of defining a nested If-Else statement in Visual Basic programming language to

execute the block of code or statements followed by another block of code based on our requirements.

Case Else // Statements to Execute if No Case Matches End Select

If you observe the above syntax, we defined a Select...Case statement with multiple case statements.

The Select statement will evaluate the expression / variable value by matching with Case statement values

(value1, value2, etc.). If the variable/expression value matches with any of the case statements, the

statements inside of that particular case will be executed.

If none of the case statements match the defined expression / variable value, then the statements inside

the Else block will be executed, and it’s more like Else block in the if...else statement.

Visual Basic Select Case Statement Flow Chart

Following is the pictorial representation of Select...Case statement process flow in Visual Basic

programming language.

If you observe the above Select...Case statement flow chart diagram, the process flow of Select..Case

statement will start from the Top to the Bottom, and in the first case, it will check whether the expression

value matches or not.

In case, if the expression value matches, it will execute the particular Case statement block and execute

the Select statement; otherwise, it will go to the second Case statement and check whether the expression

value is matching or not, the same way the search will continue till it finds the right Case statement.

If all case statements fail to match with the defined expression value, then the Else block statements will be

executed, and the Select statement will come to an end.

Visual Basic Select Case Statement Example

Following is the example of using select...case statement in Visual Basic programming language.

Module Module Sub Main() Dim x As Integer = 20 Select Case x Case 10 Console.WriteLine("x value is 10") Case 15 Console.WriteLine("x value is 15") Case 20 Console.WriteLine("x value is 20") Case Else Console.WriteLine("Not Known") End Select Console.WriteLine("Press Enter Key to Exit..") Console.ReadLine() End Sub End Module

If you observe the above example, we defined Select with multiple Case statements, and it will execute the matched Case statements with the expression value. When we execute the above Visual Basic program, we will get the result as shown below.

If you observe the above result, the Case statement ( 20 ) matches the defined expression value ( 20 ) and executes the statements within the respective Case statement.

Visual Basic Nested Select Case Statements

In Visual Basic, using one Select...Case statement within another Select...Case statement is called a nested

Select...Case statements.

Following is the example of using nested Select...Case statements in Visual Basic programming language.

Module Module Sub Main() Dim x As Integer = 10, y As Integer = 5 Select Case x Case 10 Console.WriteLine("X Value: 10") Select Case y Case 5 Console.WriteLine("Nested Switch Value: 5") Select Case y - 2 Case 3