3. Introduction
Prof. K. Adisesha
3
VB.NET :
VB.NET is a simple, multi-paradigm object-oriented programming language designed to
create a wide range of Windows, Web, and mobile applications built on the .NET
Framework.
➢ The VB.NET stands for Visual Basic. Network Enabled Technologies.
➢ It is a object-oriented programming language developed by Microsoft in 2002.
➢ It is a successor of Visual Basic 6.0, that is implemented on the Microsoft .NET
framework.
➢ It is not a case sensitive language like C , C++, Java or C#.
➢ Everything in the VB.NET language is an object, including:
❖ All primitive data types (Integer, String, char, long, short, Boolean, etc.).
❖ User-defined data types, events, and all objects that inherit from its base class.
4. Introduction
Prof. K. Adisesha
4
.NET Framework:
It is a virtual machine that provide a common platform to run an application that was
built using the different language such as C#, VB.NET, Visual Basic, etc.
➢ The main objective of this framework is to develop an application that can run on the
windows platform. The current version of the .Net framework is 4.8.
➢ There are following components of .NET Framework:
❖ CLR (Common Language Runtime)
❖ CTS (Common Type System)
❖ BCL (Base Class Library)
❖ CLS (Common Language Specification)
❖ FCL (Framework Class Library)
❖ .NET Assemblies
❖ XML Web Services
5. Introduction
Prof. K. Adisesha
5
VB.NET:
VB.NET is a simple, object-oriented programming language developed by Microsoft in
2002, and it is the successor of Visual Basic 6 (VB6) language, that implement on the
.NET Framework.
➢ The Application built using the VB.NET is much secure, robust, high-performance, and
easy to develop.
➢ There is some VB.NET application:
❖ Console Application
❖ Mobile application
❖ Windows forms Application
❖ Window control library
❖ .Net website
6. Introduction
Prof. K. Adisesha
6
VB.NET:
As we know, it is a high-level programming language with many features to develop a
secure and robust application.
➢ These are the following features of VB.Net:
❖ It is an object-oriented programming language hence everything in VB.NET programming
will be treated as an object.
❖ This language is used to design user interfaces for window, mobile, and web-based
applications.
❖ It supports a rapid application development tool kit.
❖ It provides simple events management in .NET application.
❖ A Window Form enables us to inherit all existing functionality of form that can be used to
create a new form. So, in this way, it reduced the code complexity.
❖ Automatic initialized a garbage collection.
7. Introduction
Prof. K. Adisesha
7
Visual Studio IDE:
Microsoft has provided an IDE (Integrated Development Environment) tool called Visual
Studio to build applications using programming languages such as C#, F#, Visual Basic,
etc., based on our requirements.:
❖ To install and use Visual Studio for a commercial
purpose, we need to buy a license from Microsoft.
If you want to use Visual Studio for learning (non-
commercial) purposes, Microsoft provided a free
Visual Studio Community version.
❖ After the visual studio launch, create a new console
application for that, Go to
File New select a Project .
8. Introduction
Prof. K. Adisesha
8
VB.NET:
The main functions include writing, compiling and debugging code, monitoring
resources. The IDE in VB (Visual Basic) is 'Visual Studio’ consists of the following
components:
❖ Toolbar.
❖ Menu bar.
❖ Project explorer.
❖ Properties window.
❖ Form layout window.
❖ Object designer.
❖ Form designer.
9. Introduction
Prof. K. Adisesha
9
VB.Net - Program Structure:
Before we study basic building blocks of the VB.Net programming language, let us look
a bare minimum VB.Net program structure−
➢ A VB.Net program basically consists of the following parts −
❖ Namespace declaration
❖ A class or module
❖ One or more procedures
❖ Variables
❖ The Main procedure
❖ Statements & Expressions
❖ Comments
➢ Example:
Imports System
Module Module1
'This program will display Hello World
Sub Main()
Console.WriteLine("Hello World")
Console.ReadKey()
End Sub
End Module
10. VB.Net Program
Prof. K. Adisesha
10
VB.Net - Basic Syntax:
VB.Net is an object-oriented programming language. In Object-Oriented Programming
methodology, a program consists of various objects that interact with each other by
means of actions.
❖ Object − Objects have states and behaviors. Example: A Student has states - Regno, name,
Course as well as Subjects - Theory, Lab, Projects, etc. An object is an instance of a class.
❖ Class − A class can be defined as a template/blueprint that describes the behaviors/states that
objects of its type support.
❖ Methods − A method is basically a behavior. A class can contain many methods. It is in
methods where the logics are written, data is manipulated and all the actions are executed.
❖ Instance Variables − Each object has its unique set of instance variables. An object's state is
created by the values assigned to these instance variables.
11. VB.Net Program
Prof. K. Adisesha
11
VB.Net Program Structure:
➢ VB.Net Comments: Comments can be used to explain code, and to make it more
readable. It can also be used to prevent execution when testing alternative code.
❖ You need to use a ' in front of every line you want to comment out.
❖ NET you can do Ctrl + K then C to comment, Crtl + K then U to uncomment a block.
➢ VB.Net Identifiers: Identifiers are descriptive names in order to create understandable
and maintainable code.
❖ A name must begin with a letter that could be followed by a sequence of letters, digits (0 - 9)
or underscore. The first character in an identifier cannot be a digit.
❖ It must not contain any embedded space or symbol like ? - +! @ # % ^ & * ( ) [ ] { } . ; : " ' /
However, an underscore ( _ ) can be used.
❖ It should not be a reserved keyword.
12. VB.Net Program
Prof. K. Adisesha
12
Visual Basic Keyword:
➢ VB.Net Comments: In visual basic, Keywords are the predefined set of reserved words
that have special meaning for the compiler.
➢ So the keywords in visual basic cannot be used as identifiers such as variable name,
class name, etc., in our applications.
➢ Visual Basic Keyword Types
❖ Reserved Keywords: Reserved keywords will not allow us to use them as names for
programming elements such as variables, class, etc. In case, if we want to bypass this
restriction, we need to define the variable name in brackets ([]).
❖ Unreserved Keywords: are not reserved keywords which means we can use them as names
for programming elements such as variables, class, etc. However, it’s not recommended to
use the keywords as programming elements.
➢ Example: For example, [Class] is a valid identifier but Class is not because it’s a keyword.
13. VB.Net Program
Prof. K. Adisesha
13
VB.Net program Structure:
Data Types: A data type specifies the size and type of variable values. It is important to
use the correct data type for the corresponding variable to save time and memory.
➢ The most common data types are:
Type Example
Integral types SByte, Byte, Short, UShort, Integer, UInteger, Long, ULong and Char
Floating point types Single and Double
Decimal types Decimal
Boolean types True or False values, as assigned
Date types Date
14. VB.Net program
Prof. K. Adisesha
14
VB.Net program Structure:
➢ C# Variables: Variables are containers for storing data values. Each variable in
VB.Net has a specific type, which determines the size and layout of the variable's
memory.
➢ 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
❖ Syntax: Dim [Variable Name] As [Data Type]
Dim [Variable Name] As [Data Type] = [Value]
❖ Example: Dim StudentID As Integer
Dim StudentName As String
Dim count1, count2 As Integer
Dim exitButton As New System.Windows.Forms.Button
15. VB.Net program
Prof. K. Adisesha
15
VB.Net program Structure:
➢ Variable Initialization in VB.Net: Variables are initialized (assigned a value) with an
equal sign followed by a constant expression.
➢ The general form of initialization is
➢ Syntax: variable_name = value;
❖ Example: Dim pi As Double
pi = 3.14159
Or
Dim StudentID As Integer = 100
Dim StudentName As String = “Sunny"
16. VB.Net program
Prof. K. Adisesha
16
User Input/output: The user can input or display output his or hers data, which is stored in
the variable using built-in methods.
User Input:The Console.ReadLine() method returns a string.
❖ // Create a string variable and get user input from the keyboard and store it in the variable
string userName = Console.ReadLine();
User Output:Console.WriteLine() is used to output (print) values..
❖ // Print the value of the variable (userName), which will display the input value
Console.WriteLine("Username is: " + userName);
❖ Example: Imports System
Module Module1
Sub Main()
Console.WriteLine("Press Enter Key to Exit.")
Console.ReadLine()
End Sub
End Module
17. VB.Net program
Prof. K. Adisesha
17
Visual Basic Operator :
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.
❖ Arithmetic Operators
❖ Assignment Operators
❖ Logical/Bitwise Operators
❖ Comparison Operators
❖ Concatenation Operators
18. Operator Description Example (a = 6, b = 3)
+ It will add two operands. a + b = 9
- It will subtract two operands. a - b = 3
* It will multiply two operands. a * b = 18
/ It divides two numbers and returns a floating-point result. a / b = 2
It divides two numbers and returns an integer result. a b = 2
Mod It divides two numbers and returns only the remainder. a Mod b = 0
^ It raises a number to the power of another number. a ^ b = 216
VB.Net program
Prof. K. Adisesha
18
Arithmetic Operators:
In Visual Basic, Arithmetic Operators are useful for performing basic arithmetic
calculations like addition, subtraction, division, etc., based on our requirements.
19. Operator Description Example
= It will assign a value to a variable or property. a = 10
+= It will add left and right operands and assign a result to the left operand. a += 10 equals to a = a + 10
-= It will subtract left and right operands and assign a result to the left operand. a -= 10 equals to a = a - 10
*= It will multiply left and right operands and assign a result to the left operand. a *= 10 equals to a = a * 10
/= It will divide left and right operands and assign the floating-point result to the
left operand.
a /= 10 equals to a = a / 10
= It will divide left and right operands and assign the integer result to the left
operand.
a = 10 equals to a = a 10
^= It will raise the value of a variable to the power of expression and assign the
result back to the variable.
a ^= 10 equals to a = a ^ 10
&= It will concatenate a String expression to a String variable and assign the result
to the variable.
a &= “BCA" equals to a = a
& “BCA"
VB.Net program
Prof. K. Adisesha
19
Assignment Operators: In Visual Basic, Assignment Operators are useful to assign
a new value to the operand.
20. 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.
VB.Net program
Prof. K. Adisesha
20
Logical / Bitwise Operators:
The Logical / Bitwise Operators will always work with Boolean expressions (true or false)
21. Operator Description Example
== Checks if the values of two operands are equal or not, if yes then condition becomes true. (A == B) is false
<> Checks if the values of two operands are equal or not, if values are not equal then
condition becomes true.
(A <> B) is true.
> Checks if the value of left operand is greater than the value of right operand (A > B) is false
< Checks if the value of left operand is less than the value of right operand (A < B) is true.
>= Checks if the value of left operand is greater than or equal to the value of right operand (A >= B) is false
<= Checks if the value of left operand is less than or equal to the value of right operand, (A <= B) is true.
IsNot It will return true if two object references refer to different objects.
VB.Net program
Prof. K. Adisesha
21
Relational Operators:
Following table shows all the relational operators supported by C#.
➢ Assume variable A holds 10 and variable B holds 20 then −.
22. VB.Net program
Prof. K. Adisesha
22
Operators:
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 = BCA-, b = Course)
& It will concatenate given two expressions. a & b = BCA-Course
+ It is useful to add two numbers or concatenate
two string expressions.
a + b = BCA-Course
23. VB.Net program
Prof. K. Adisesha
23
Control Structures:
Conditional statements: There are few conditions statements to perform different actions
for different decisions that are supported by C#.
➢ Visual Basic has the following conditional statements:
❖ if :Use if to specify a block of code to be executed, if a specified condition is true
❖ If-else : Use else to specify a block of code to be executed, if the condition is false
❖ If-else if :Use else if to specify a new condition to test, if the first condition is false
❖ Select Case : Use switch to specify many alternative blocks of code to be executed
Syntax
If bool_expression Then
// Statements to Execute if condition is true
End If
Example
Dim x As Integer = 20
If x >= 10 Then
Console.WriteLine("Number Greater than 10")
End If
24. VB.Net program
Prof. K. Adisesha
24
Visual Basic If Else Statement:
In Visual Basic, If Else statement or condition has an optional Else statements, the Else
statements will be executed whenever the If condition fails to execute.
➢ If Else statement, whenever the boolean expression returns true, the If statements will be
executed; otherwise, the Else block of statements will be executed.
Syntax: Example:
If boolean_expression Then
// Statements to Execute if boolean expression is True
Else
// Statements to Execute if boolean expression is False
End If
Dim x As Integer = 20
If x >= 10 Then
Console.WriteLine("x is Greater then10")
Else
Console.WriteLine("x is less than 10")
End If
25. VB.Net program
Prof. K. Adisesha
25
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.
➢ 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. Example:
Syntax:
If condition_1 Then
// Statements to Execute if condition_1 is True
ElseIf condition_2 Then
// Statements to Execute if condition_2 is True
....
Else
// Statements to Execute if all conditions are False
End If
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
26. VB.Net program
Prof. K. Adisesha
26
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:
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
27. VB.Net program
Prof. K. Adisesha
27
Ternary Operator:
In Visual Basic, Ternary Operator is a decision-making operator, and it is an alternative
for the if…else statement in Visual Basic programming language.
➢ In Visual Basic, the Ternary Operator will always work with three operands.
Syntax:
Example:
If((condition_expression), first_expression, second_expression)
Module Module1
Sub Main()
Dim x As Integer = 5, y As Integer = 20
Dim result As String
result = If((x > y), "x value greater than y", "x value less than y")
Console.WriteLine(result)
Console.ReadLine()
End Sub
End Module
28. VB.Net program
Prof. K. Adisesha
28
Select Case Statement:
In Visual Basic, Select...Case statement is useful to execute a single case statement from
the group of multiple case statements based on the value of a defined expression.
➢ By using Select...Case statement in Visual Basic, we can replace the functionality of
if…else if statement to provide better readability for the code.
Syntax:
Select Case
variable/expresison
Case value1
// Statements to Execute
Case value2
//Statements to Execute
....
....
Case Else
// Execute if No Case
Matches
End Select
Example
Module Module1
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
29. VB.Net program
Prof. K. Adisesha
29
Looping Structures:
VB.Net - Loops: Loops can execute a block of code as long as a specified condition is
reached.
➢ Visual Basic provides following types of loop to handle looping requirements.
Loop Type Description
while loop It repeats a statement or a group of statements while a given condition is true.
It tests the condition before executing the loop body.
for loop It executes a sequence of statements multiple times and abbreviates the code that
manages the loop variable.
do...while loop It is similar to a while statement, except that it tests the condition at the end of the
loop body
For each loops You can use one or more loop inside any another while, for or do..while loop.
30. VB.Net program
Prof. K. Adisesha
30
For- Loops:
In Visual Basic, For loop is useful to execute a statement or a group of statements
repeatedly until the defined condition returns true.
➢ For loop is useful in Visual Basic applications to iterate and execute a certain block of
statements repeatedly until the specified number of times.
❖ Syntax :
For variable As [Data Type] = start To end
// Statements to Execute
Next
Example:
Module Module1
Sub Main()
For i As Integer = 1 To 4
Console.WriteLine("i value: {0}", i)
Next
Console.WriteLine("Press Enter Key to Exit..")
Console.ReadLine()
End Sub
End Module
31. VB.Net program
Prof. K. Adisesha
31
Nested For Loop:
In visual basic, we can create one For loop within another For loop based on our
requirements.
➢ Following is the example of creating a nested For loop in Visual Basic.
Example:
Module Module1
Sub Main()
For i As Integer = 1 To 4
For j As Integer = i To 3 - 1
Console.WriteLine("i value: {0}, j value: {1}", i, j)
Next
Next
Console.WriteLine("Press Enter Key to Exit..")
Console.ReadLine()
End Sub
End Module
32. VB.Net program
Prof. K. Adisesha
32
While Loop:
In Visual Basic, While loop is useful to execute the block of statements as long as the
specified condition is true.
➢ In case we are unknown about the number of times to execute the block of statements, then
While loop is the best solution.
❖ Syntax :
While boolean_expression
// Statements to Execute
End While
Example:
Module Module1
Sub Main()
Dim i As Integer = 1
While i <= 4
Console.WriteLine("i value: {0}", i)
i += 1
End While
Console.WriteLine("Press Enter Key to Exit..")
Console.ReadLine()
End Sub
End Module
33. VB.Net program
Prof. K. Adisesha
33
While Loop with Exit Statement:
In Visual Basic, we can exit or terminate the execution of While loop immediately by using
Exit keyword.
➢ Following is the example of using Exit keyword in While loop to terminate the execution of
loop in Visual Basic programming language.
❖ Syntax : Example:
While boolean_expression
// Statements to Execute
If <cond> Then Exit While
End While
Module Module1
Sub Main()
Dim i As Integer = 1
While i < 4
Console.WriteLine("i value: {0}", i)
i += 1
If i = 2 Then Exit While
End While
Console.WriteLine("Press Enter Key to Exit..")
Console.ReadLine()
End Sub
End Module
34. VB.Net program
Prof. K. Adisesha
34
Do While Loop:
In Visual Basic, the do-while loop is same as the while loop, but the only difference is while
loop will execute the statements only when the defined condition returns true.
➢ The do-while loop will execute the statements at least once because first it will execute the
block of statements and then it will checks the condition.
➢ In Visual Basic, we can exit or terminate the execution of the Do-While loop immediately
by using Exit keyword also.
❖ Syntax :
Do
// Statements to Execute
Loop While boolean_expression
Example:
Sub Main()
Dim i As Integer = 1
Do
Console.WriteLine("i value: {0}", i)
i += 1
Loop While i <= 4
Console.WriteLine("Press Enter Key to Exit..")
Console.ReadLine()
End Sub
35. VB.Net program
Prof. K. Adisesha
35
For Each Loop:
In Visual Basic, For Each loop is useful to loop through items in an array or collection
object to repeatedly execute the block of statements.
➢ In Visual Basic For Each loop will work with the collection objects such as an array, list,
etc., to execute the block of statements for each element in the array or collection.
➢ In Visual Basic, we can use Exit, Continue statements within For Each loop to exit or
continue to the next iteration of the loop based on our requirements.
❖ Syntax :
For Each var_name As [Data_Type] In Collection_Object
// Statements to Execute
Next
Example:
Dim names As String() = New String(2) {
«Prajwal", «Sunny", «Shailu"}
For Each name As String In names
Console.WriteLine(name)
Next
36. VB.Net program
Prof. K. Adisesha
36
Visual Basic- Jump Statements:
➢ Exit Statement: Is useful to terminate the execution of loops and transfers the control
immediately to the next statements that follow a terminated loops or statements.
➢ VB Continue Statement: Is useful to transfer the control immediately to the next
iteration of loops.
➢ GoTo Statement: Is useful to transfer the program control to the specified labeled
statement.
➢ Return Statement: Is useful to terminate the execution of the method in which it
appears and return the control back to the calling method
37. VB.Net program
Prof. K. Adisesha
37
Exit Statement:
In visual basic, the Exit statement is useful to terminate the execution of loops (for, while,
do-while, etc.) and transfers the control immediately to the next statements that follow a
terminated loops or statements.
➢ Following is the syntax of defining the Exit statement in a visual basic programming
language. Exit { Do | For | Function | Property | Select | Sub | Try | While }.
❖ example of terminating the execution of for loop with Exit statement
Sub Main()
For i As Integer = 1 To 4
If i = 3 Then Exit For
Console.WriteLine("i value: {0}", i)
Next
Console.WriteLine("Press Enter Key to Exit..")
Console.ReadLine()
End Sub
38. VB.Net program
Prof. K. Adisesha
38
Continue Statement:
In visual basic, the Continue statement is useful to transfer the control immediately to
the next iteration of loops such as For, While, Do-While from the specified position by
skipping the remaining code.
➢ The main difference between the Exit statement and Continue statement is, the Exit
statement will completely terminate the loop or statement execution but the Continue
statement will transfer the control immediately to the next iteration of the loop
➢ Following is the syntax of defining the Continue statement in the visual basic
programming language.
Continue { Do | For | While }
For i As Integer = 1 To 4
If i = 3 Then Continue For
Console.WriteLine("i value: {0}", i)
Next
39. VB.Net program
Prof. K. Adisesha
39
GoTo Statement:
n visual basic, the GoTo statement is useful to transfer the program control to the
specified labeled statement. It is useful to get out of the loop or exit from deeply nested
loops based on our requirements.
➢ We can also define the multiple GoTo statements in our application to transfer the
program control to the specified labeled statement.
➢ Syntax:
GoTo labeled_statement;
For i As Integer = 1 To 10 - 1
If i = 5 Then GoTo endloop
End If
Console.WriteLine("i value: {0}", i)
Next
endloop: Console.WriteLine("The end")
40. VB.Net program
Prof. K. Adisesha
40
Return Statement:
In visual basic, the Return statement is useful to terminate the execution of the method
in which it appears and return the control back to the calling method.
➢ The Return statement is useful whenever we want to value the other methods. We can
also omit the usage of return statement in our methods by using void as a return type.
➢ Syntax:
Return return_val;
Public Function SumofN(ByVal a As Integer, ByVal b As Integer) As Integer
Dim x As Integer = a + b
Return x
End Function
➢ Example:
41. VB.Net program
Prof. K. Adisesha
41
Visual Basic Arrays:
Arrays are useful to store multiple elements of the same data type at contiguous memory
locations and arrays will allow us to store the fixed number of elements sequentially based
on the predefined number of items.
➢ Arrays Declaration: Arrays can be declared by specifying the type of elements followed by
the brackets () like as shown below−
❖ Syntax : Dim array_name As [Data_Type]();
❖ Example : ' Store only int values
Dim numbers As Integer()
➢ You can also create and initialize an array, as shown −
❖ Example : ' Declaring and Initializing an array with size of 5
Dim array As Integer() = New Integer(4) {}
' Defining and assigning an elements at the same time
Dim array2 As Integer() = New Integer(4) {1, 2, 3, 4, 5}
42. VB.Net program
Prof. K. Adisesha
42
Multidimensional Arrays:
In visual basic, a multidimensional array is an array that contains more than one
dimension to represent the elements in a tabular format like rows and columns.
➢ Multidimensional arrays can support either two or three-dimensional series. To create
multi-dimensional arrays, we need to use comma (,) separator inside of the brackets.
➢ To create a 2D & 3D array, add each array within its own set of curly braces, and insert a
comma (,) inside the square brackets.
❖ Syntax : ' Two Dimensional Array
Dim arr As Integer(,) = New Integer(3, 1) {}
' Three Dimensional Array
Dim arr1 As Integer(,,) = New Integer(3, 1, 2) {}
43. VB.Net program
Prof. K. Adisesha
43
Multi-Dimensional Array Initialization:
In visual basic, we can initialize arrays upon declaration. Following are the different ways
of declaring and initializing the multidimensional arrays in visual basic programming
language.
➢ Examples, we declared and initialized two dimensional array with 3 rows and 2 columns
and three dimensional array with 2, 2, 3 dimensions.
❖ Syntax : ' Two Dimensional Array
Dim intarr As Integer(,) = New Integer(2, 1) {{4, 5}, {5, 0}, {3, 1}}
' Three Dimensional Array
Dim array3D As Integer(,,) = New Integer(1, 1, 2) {{{1, 2, 3}, {4, 5, 6}},
{{7, 8, 9}, {10, 11, 12}}}
44. VB.Net program
Prof. K. Adisesha
44
Jagged Arrays:
In visual basic, Jagged Array is an array whose elements are arrays with different
dimensions and sizes. Sometimes the a jagged array called as “array of arrays” and it can
store arrays instead of a particular data type value.
➢ The jagged array can be initialized with two brackets ()(). The first bracket will specify the
size of an array and the second one will specify the dimension of an array which is going
to be stored as value.
❖ Syntax : ' Jagged Array with Single Dimensional Array
Dim jarray As Integer()() = New Integer(1)() {}
' Jagged Array with Two Dimensional Array
Dim jarray1 As Integer()(,) = New Integer(2)(,) {}
45. VB.Net program
Prof. K. Adisesha
45
Jagged Array Initialization:
The following are the different ways of declaring and initializing the jagged arrays in a
visual basic programming language.
➢ In jagged arrays the size of elements is optional. So, for some elements we didn’t mention
the size to store single or multidimensional arrays with different sizes based on our
requirements.
❖ Syntax : ' Jagged Array with Single Dimensional Array
Dim jarray As Integer()() = New Integer(2)() {}
jarray(0) = New Integer(4) {1, 2, 3, 4, 5}
jarray(1) = New Integer(2) {10, 20, 30}
jarray(2) = New Integer() {12, 50, 60, 70, 32}
46. VB.Net program
Prof. K. Adisesha
46
Visual Basic String:
In visual basic, String is a keyword and it is useful to represent a sequential collection of
characters that is called a text. The String keyword is useful to create a string variable to
hold the text which is a sequential collection of characters..
➢ In visual basic, the string is immutable, it means the string object cannot be modified once
it created.
➢ To access Individual Characters from Strings:
Dim name As String = “Hello Sunny"
For i As Integer = 0 To name.Length - 1
Console.Write(name(i))
Next Outputs : Hello Sunny!
47. Method Description
Compare(String, String) It compares two specified String objects and returns an integer that indicates their relative
position in the sort order.
Concat(String, String) It concatenates two specified instances of String.
Copy(String) It creates a new instance of String with the same value as a specified String.
Format(String, Object) It replaces one or more format items in a specified string with the string representation of a
specified object.
Trim() It removes all leading and trailing white-space characters from the current String object.
ToLower() It converts a given string to lowercase.
ToUpper() It converts a given string to uppercase.
Split(Char[]) It splits a string into substrings that are based on the characters in an array.
VB.Net program
Prof. K. Adisesha
47
String Methods:
In visual basic, the string class contains various methods to manipulate the string objects
data based on our requirements.
48. VB.Net program
Prof. K. Adisesha
48
String Methods:
In visual basic, the string class contains various methods to manipulate the string objects
data based on our requirements.
➢ String Split Method: Is useful to split a string into substrings based on the characters in an
array.
❖ Syntax: Public Function Split(ByVal separator As Char()) As String()
➢ String Concat method: is useful to concatenate or append one string to the end of another
string and return a new string.
❖ Syntax: Public Function Concat(ByVal string1 As String, ByVal string2 As String) As String
➢ String Replace method: is useful to replace the specified string or a character in all
occurrences of a given string.
❖ Syntax: Dim x As String = "aaaaa“
Dim nx As String = x.Replace("a", "b").Replace("b", "c")
49. VB.Net program
Prof. K. Adisesha
49
String Methods:
In visual basic, the string class contains various methods to manipulate the string objects
data based on our requirements.
➢ String Compare method: is useful to compare two specified strings and return an integer
value that indicates their relative position in the sort order.
❖ Syntax: Public Function Compare(ByVal string1 As String, ByVal string2 As String) As Integer
➢ String Format method is useful to insert the value of variable or an object or expression
into another string.
❖ Syntax: Public Function Format(ByVal _ As String, ByVal _ As Object) As String
➢ String Copy method: is useful to create a new instance of string object with the same
content of the specified string object.
❖ Syntax: Public Shared Function Copy(ByVal str As String) As String
50. VB.Net program
Prof. K. Adisesha
50
Visual Basic OOP:
OOP stands for Object-Oriented Programming, which is about creating objects that
contain both data and methods.
➢ Object-oriented programming has several advantages over procedural programming:
❖ OOP is faster and easier to execute
❖ OOP provides a clear structure for the programs
❖ OOP helps to keep the code DRY "Don't Repeat Yourself", and makes the code easier
to maintain, modify and debug
❖ OOP makes it possible to create full reusable applications with less code and shorter
development time
51. VB.Net program
Prof. K. Adisesha
51
Object and Class:
Visual Basic is an object-oriented language, program is designed using objects and classes.
➢ VB Object: Object is an instance of a Class and that can be used to access the data
members and member functions of a class.
❖ Example: Dim user As Users = New Users()
➢ VB Class : Class is a data structure and it will combine the various types of data members
such as fields, properties, member functions and events into a single unit.
❖ Syntax:
Public Class users
' Properties, Methods, Events, etc.
End Class
52. VB.Net program
Prof. K. Adisesha
52
Access Modifiers:
In visual basic, Access Modifiers are the keywords and those are useful to define an
accessibility level for all the types and type members.
➢ By specifying the access level for all the types and type members, we can control whether
they can be accessed in other classes or in current assembly or in other assemblies based
on our requirements.
➢ The following are the different types of access modifiers available in a visual basic:
❖ Public
❖ Private
❖ Protected
❖ Friend (internal in c#)
Class User
Public Name As String
Public Location As String
Public Sub GetUserDetails()
Console.WriteLine("Name: {0}", Name)
Console.WriteLine("Location: {0}", Location)
End Class
53. VB.Net program
Prof. K. Adisesha
53
Access Modifiers:
By using these four access modifiers, we can specify a following six levels of accessibility
for all types and type members based on our requirements.
Access Modifier Description
Public It is used to specifies that access is not restricted.
Private It is used to specifies that access is limited to the containing type.
Protected It is used to specifies that the access is limited to the containing type or types
derived from the containing class.
Friend (internal) It is used to specifies that access is limited to the current assembly.
Protected Friend It is used to specifies that access is limited to the current assembly or types
derived from the containing class.
54. VB.Net program
Prof. K. Adisesha
54
Inheritance:
Inheritance is one of the primary concepts of object-oriented programming (OOP) and it is
useful to inherit the properties from one class (base) to another (child) class.
➢ In visual basic inheritance, the class whose members are inherited is called a base
(parent) class and the class that inherits the members of base (parent) class is called a
derived (child) class.
❖ Example: <access_modifier> Class <base_class_name>
// Base class Implementation
End Class
<access_modifier> Class <derived_class_name>
Inherits base_class_name
// Derived class implementation
End Class
55. VB.Net program
Prof. K. Adisesha
55
C# - Polymorphism:
In object-oriented programming paradigm, polymorphism is often expressed as 'one
interface, multiple functions'.
➢ Polymorphism can be static or dynamic.
❖ In static polymorphism, the response to a function is determined at the compile time.
❖ In dynamic polymorphism, it is decided at run-time..
➢ Static Polymorphism: The mechanism of linking a function with an object during compile
time is called early binding. It is also called static binding.
➢ C# provides two techniques to implement static polymorphism:
❖ Method Overloading
❖ Operator overloading
56. VB.Net program
Prof. K. Adisesha
56
Method Overloading:
Method Overloading means defining multiple methods with the same name but with
different parameters.
➢ By using Method Overloading, we can perform different tasks with the same method name by passing
different parameters.
❖ By changing number of arguments
❖ By changing data type of the arguments.
➢ Example: Public Class Calculate
Public Sub AddNumbers(ByVal a As Integer, ByVal b As Integer)
Console.WriteLine("a + b = {0}", a + b)
End Sub
Public Sub AddNumbers(ByVal a As Integer, ByVal b As Integer, ByVal c As Integer)
Console.WriteLine("a + b + c = {0}", a + b + c)
End Sub
End Class
57. VB.Net program
Prof. K. Adisesha
57
Method Overriding:
Method Overriding means override a base class method in the derived class by creating a
method with the same name and signatures to perform a different task.
➢ The Method Overriding in visual basic can be achieved by using Overridable & Overrides
keywords along with the inheritance principle..
➢ Method Overriding is also called as run time polymorphism or late binding.
➢ Example, 'Derived Class
Public Class Details
Inherits Users
Public Overrides Sub GetInfo()
Console.WriteLine("Derived Class")
End Sub
End Class
' Base Class
Public Class Users
Public Overridable Sub GetInfo()
Console.WriteLine("Base Class")
End Sub
End Class
58. VB.Net program
Prof. K. Adisesha
58
Visual Basic Interface:
Interface is same as a class but the only difference is class can contain both declarations
and implementation of methods, properties, and events but Interface will contain only the
declarations of methods, properties, and events that a class or structure can implement.
➢ visual basic will not support multiple inheritance of classes but that can be achieved by using
Interface. We can define the interface by using Interface keyword .
➢ To implement an interface in a class or structure, the syntax will be like
➢ Syntax:
Interface IUser
Sub GetDetails()
End Interface
Example: // Interface
Class User
Inherits IUser
Private Sub GetDetails() Implements IUser.GetDetails
' Method Implementation
End Sub
End Class
59. VB.Net program
Prof. K. Adisesha
59
Visual Basic Delegates:
In visual basic, the delegate is a type that defines a method signature and it is useful to
hold the reference of one or more methods that is having the same signatures.
➢ By using delegates, we can invoke the methods and send methods as an argument to other
methods.
➢ In visual basic, the delegate is a reference type and it’s type-safe and secure. The delegates
are similar to function pointers in c++.
➢ Syntax:
<access_modifier> Delegate <return_type> <delegate_name>(<parameters>)
➢ Example:
Public Delegate Sub UserDetails(ByVal name As String);
60. VB.Net program
Prof. K. Adisesha
60
Exception Handling:
In visual basic, Exception is an unexpected event or an error which may occur during the
execution of a program and it will provide necessary information about the error which
occurred in our application.
➢ Generally, the exceptions are thrown by .NET framework CLR (Common Language
Runtime) or by the code in our program. Module Module1
Sub Main(ByVal args As String())
Dim name As String = Nothing
' Null Reference Exception Error
If name.Length > 0 Then
Console.WriteLine("Name: " & name)
End If
End Sub
End Module
➢ If you observe the example, we assigned a
Nothing value to the name variable and trying to
validate the length of string variable.
61. VB.Net program
Prof. K. Adisesha
61
Exception Class:
In visual basic, the Exception class further classified into two other classes called
SystemException and ApplicationException to handle the exceptions.
➢ In visual basic, the Exception class
further classified into two other
classes called :
❖ SystemException
❖ ApplicationException
62. VB.Net program
Prof. K. Adisesha
62
Exception Classes:
Following table lists some of the important exception classes available in visual basic.
Exception Description
AccessViolationException This exception will raise when there is an attempt to read or write protected memory.
ArgumentException This exception will be thrown when one of the arguments provided to a method is not valid.
ArgumentNullException This exception will occur when null argument is passed to a method that does not accept it as a
valid argument.
ArgumentOutOfRangeException This exception will occur when the value of an argument is outside the allowable range of values.
ArithmeticException This exception will occur for an errors in arithmetic, casting, or conversion operation.
DivideByZeroException This exception will occur when we try to divide an integral or decimal value by zero.
FormatException This will occur when the format of an argument is invalid.
IndexOutOfRangeException This will occur when we try to access an element of an array or collection with an index that is
outside of its bounds.