3 VB - Net Notes
3 VB - Net Notes
UNIT - 3
Introduction to VB.NET
Maneuverings the Toolbar Auto-hide
Docking: Docking refers to how much space you want the control to take up on the form. If you dock a
control to the left of the form, it will stretch itself to the height of the form, but its width will stay the same.
Placing and Resizing the Windows Forms: Resize with the designer by dragging either the right edge, bottom
edge, or the corner, you can resize the form. The second way you can resize the form while the designer is open,
is through the properties pane. Select the form, then find the Properties pane in Visual Studio. Scroll down to size
and expand it.
Properties Window
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
1. Right-click the mouse on the control you want to create an event procedure for and select Edit Event Procedure
from the pop-up menu.
2. Choose the action (event) you want to associate with the control from the secondary pop-up menu that appears.
The Power COBOL Editor window will appear.
3. In the Editor window, 'drag and drop' related controls and write the COBOL statements to describe the actions
associated with the selected control or form. You can also select potential properties and methods to be applied
to the control or form. See "Writing an Event Procedure" and "Inserting Properties and Methods" for more
details on writing event procedures.
4. Save and close the Editor window to save the event procedure.
Keywords
A keyword is a reserved word with special meanings in the compiler, whose meaning cannot be changed.
Therefore, these keywords cannot be used as an identifier in VB.NET programming such as class name, variable,
function, module, etc.
• Reserved Keywords
• Unreserved Keywords
• Reserved keywords cannot be used as names for programming elements such as variables, procedures, etc.
You can bypass this restriction by enclosing the name in brackets ([]).
• Here is the list of the most commonly used reserved keywords.
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
Unreserved Keywords
Unreserved keywords can be used as names for your programming elements. However, it is not
recommended because it can make your code hard to read and can lead to subtle errors that can be difficult to
find.
#Region
In VB.NET, data type is used to define the type of a variable or function in a program. Furthermore, 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 so that a variable
can hold a defined data type value. For example, when we declare a variable, we have to tell the compiler what
type of data or value is allocated to different kinds of variables to hold different amounts of space in computer
memory.
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.
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
The following table shows the various data types list in the VB.NET programming language.
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
Data_type.vb
Module Data_type
Sub Main()
Dim b As Byte = 1
Dim si As Single
Dim db As Double
Dim c As Char
b=1
num = 20
si = 0.12
db = 2131.787
get_date = Today
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
c = "A"
Console.ReadKey()
End Sub
End Module
Output:
Byte is: 1
Character is: 1
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
DB_Conversion.vb
Option Strict On
Module DB_Conversion
Sub Main()
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
dblData = 5.78
Dim A, B As Char
A = "A"
B = "B"
B_int = AscW(B)
x=1
Z = AscW(A)
Z=Z+x
num = CInt(dblData)
Console.ReadKey()
End Sub
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
End Module
Output:
Ascii value of B is 66
String to integer 66
Double to Integer: 6
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
1. If-Then Statement: The If-Then Statement is a control statement that defines one or more conditions,
and if the particular condition is satisfied, it executes a piece of information or statements.
Syntax:
If condition Then
[Statement or block of Statement]
End If
In If-Then Statement, the condition can be a Boolean, logical, or relational condition, and the statement
can be single or group of statements that will be executed when the condition is true.
Example 1: Write a simple program to print a statement in VB.NET.
Module1.vb
Module Module1
' Declaration of variable str
Dim str As String = "JavaTpoint"
Sub Main()
' if str equal to "JavaTpoint", below Statement will be executed.
If str = "JavaTpoint" Then
Console.WriteLine("Welcome to the JavaTpoint")
End If
Console.WritLine("press any key to exit?")
Console.ReadKey()
End Sub
End Module
Now compile and execute the above program by clicking on the Start or F5 button, it shows the following output:
As we can see in the above example, if the value of str is equal to JavaTpoint, the condition is true, and
it prints the Statement.
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
2. If-Then-Else Statement: The If-Then Statement can execute single or multiple statements when the
condition is true, but when the expression evaluates to false, it does nothing. So, here comes the If-Then-
Else Statement. The IF-Then-Else Statement is telling what If condition to do when if the statement is false,
it executes the Else statement. Following is the If-Then-Else statement syntax in VB.NET as follows:
Syntax:
If (Boolean_expression) Then
'This statement will execute if the Boolean condition is true
Else
'Optional statement will execute if the Boolean condition is false
End If
Flow chart
The above diagram represents that if the
Boolean expression (condition) is true, the if
statement will execute, and if the Boolean
expression is false, Else code or statement will
be executed. After that, the control transfer to the
next statement, which is immediately after the If-
Then-Else control statement.
Example 1:
Write a program to check whether the
number is even or odd.
If_Else_statment.vb
Module If_Else_statement
Sub Main()
Dim num As Integer
Console.WriteLine("Enter the Number")
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
Flowchart
The following diagram represents the functioning of the If-Else-If Statement in the VB.NET programming
language.
If this condition is true in the
flowchart of the if-else-if statement, the
statement is executed within the if
block. If the condition is not true, it
passes control to the next ElseIf
condition to check whether the
condition is matched. And if none of
the conditions are matched, the else
block is executed.
Example 1:
Write a program to show the
uses of If... ElseIf statements.
if_elseIf.vb
Module if_elseIf
Sub Main()
Dim var1 As Integer
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
Case value2 'defines the item or value that you want to match.
// Define a statement to execute
Case Else
// Define the default statement if none of the conditions is true.
End Select
Furthermore, you can also set more than one condition in a single case statement, such as:
Select Case Variable / expression
Case value1
Statement1
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
Case Else
// define the default statement if none of the condition is true
End Select
Flowchart of Select Case Statement
The following flowchart represents the functioning of the Select case statement in the VB.NET programming
language.
In Flowchart, the Select Case statement represents the
evaluating of the process start from top to bottom. If the
expression or value is matched with the first select case,
statement -1 is executed else the control transfer to the next
case for checking whether the expression is matching or not.
Similarly, it checks all Select case statements for evaluating.
If none of the cases are matched, the Else block statement
will be executed, and finally, the Select Case Statement will
come to an end.
Example 1:
Write a program to display the Days name using the
select case statement in VB.NET.
Select_case.vb
Imports System
Module Select_case
Sub Main()
'define a local variable.
Dim Days As String
Days = "Thurs"
Select Case Days
Case "Mon"
Console.WriteLine(" Today is Monday")
Case "Tue"
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
End Select
Console.WriteLine("You have selected : {0}", Days)
Console.WriteLine("Press any key to exit...")
Console.ReadLine()
End Sub
End Module
Now compile and execute the above program by clicking on the Start or F5 button, it shows the following output:
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
VB.NET Loop
A Loop is used to repeat the same process multiple times until it meets the specified condition in a
program. By using a loop in a program, a programmer can repeat any number of statements up to the desired
number of repetitions. A loop also provides the suitability to a programmer to repeat the statement in a program
according to the requirement. A loop is also used to reduce the program complexity, easy to understand, and
easy to debug.
Advantages of VB.NET loop.
1. It provides code iteration functionality in a program.
2. It executes the statement until the specified condition is true.
3. It helps in reducing the size of the code.
4. It reduces compile time.
Types of Loops
There are five types of loops available in VB.NET:
1. Do While Loop
2. For Next Loop
3. For Each Loop
4. While End Loop
5. With End Loop
1. Do While Loop
In VB.NET, Do While loop is used to execute blocks of statements in the program, as long as the condition
remains true. It is like the While End Loop, but there is slight difference between them. The while loop initially
checks the defined condition, if the condition becomes true, the while loop's statement is executed. Whereas in
the Do loop, is opposite of the while loop, it means that it executes the Do statements, and then it checks the
condition.
Syntax:
Do
[ Statements to be executed]
Loop While Boolean_expression
// or
Do
[Statement to be executed]
Loop Until Boolean_expression
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
In the above syntax, the Do keyword followed a block of statements, and While keyword
checks Boolean_expression after the execution of the first Do statement.
Flowchart of Do loop.
The above flow chart represents the flow of
Do While loop. It is used to control the flow
of statements, such that it executes the
statement at least once before checking the
While or Until condition. If the condition is
true, the next iteration will be executed till the
condition become false.
Example 1. Write a simple program to print
a number from 1 to 10 using the Do While
loop in VB.NET.
Do_loop.vb
Imports System
Module Do_loop
Sub Main()
' Initializatio and Declaration of variable i
Dim i As Integer = 1
Do
' Executes the following Statement
Console.WriteLine(" Value of variable I is : {0}", i)
i = i + 1 'Increment the variable i by 1
Loop While i <= 10 ' Define the While Condition
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
Now compile and execute the above program by clicking on the Start button, it shows the following output:
In the above program, the Do While loop executes the body until the given condition becomes false. When
the condition becomes false the loop will be terminated.
2. Do Until Loop:
In the VB.NET loop, there is a Do Until loop statement, which is similar to the Do While loop. The Do
Statement executes as long as Until condition becomes true.
Example: Write a loop.vb
Imports System
Module Do_loop
Sub Main()
' Initialization and Declaration of variable i
Dim i As Integer = 1
Do
' Executes the following Statement
Console.WriteLine(" Value of variable i is : {0}", i)
i = i + 1 'Increment variable i by 1
Loop Until i = 10 ' Define the Until Condition
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
Output:
In the above program, a Do Until loop is executed their statement until the given condition Until (i =10) is
not meet. When the counter value of the variable i becomes 10, the defined statement will be false, and the loop
will be terminated.
3. Nested Do While Loop Statement
In VB.NET, when we use one Do While loop inside the body of another Do While loop, it is called Nested
Do While loop.
Syntax
Do
// Statement of the outer loop
Do
// Statement of outer loop
While (condition -2)
// Statement of outer loop
While (condition -1)
4. For Next Loop
A For Next loop is used to repeatedly execute a sequence of code or a block of code until a given condition
is satisfied. A For loop is useful in such a case when we know how many times a block of code has to be executed.
In VB.NET, the For loop is also known as For Next Loop.
Syntax
For variable_name As [ DataType ] = start To end [ Step step ]
[ Statements to be executed ]
Next
Let us understand the For Next loop in detail.
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
Example 1. Write a simple program to print the number from 1 to 10 using the For Next loop.
Number.vb
Imports System
Module Number
Sub Main()
' It is a simple print statement, and 'vbCrLf' is used to jump in the next line.
Console.Write(" The number starts from 1 to 10 " & vbCrLf)
' declare and initialize variable i
For i As Integer = 1 To 10 Step 1
' if the condition is true, the following statement will be executed
Console.WriteLine(" Number is {0} ", i)
' after completion of each iteration, next will update the variable counter
Next
Console.WriteLine(" Press any key to exit... ")
Console.ReadKey()
End Sub
End Module
Output:
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
VB.NET Arrays
An array is a linear data structure that is a collection of data elements of the same type stored on
a contiguous memory location. Each data item is called an element of the array. It is a fixed size of sequentially
arranged elements in computer memory with the first element being at index 0 and the last element at index n -
1, where n represents the total number of elements in the array.
The following is an illustrated representation of similar data type elements defined in the VB.NET array data
structure.
In the above diagram, we store the Integer type data elements in an array starting at index 0. It will continue
to store data elements up to a defined number of elements.
Declaration of VB.NET Array
We can declare an array by specifying the data of the elements followed by parentheses () in the VB.NET.
Dim array_name As [Data_Type] ()
In the above declaration, array_name is the name of an array, and the Data_Type represents the type of
element (Integer, char, String, Decimal) that will to store contiguous data elements in the VB.NET array.
Now, let us see the example to declare an array.
'Store only Integer values
Dim num As Integer() or Dim num(5) As Integer
'Store only String values
Dim name As String() or Dim name(5) As String
' Store only Double values
Dim marks As Double()
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
Multidimensional Array
In VB.NET, a multidimensional array is useful for storing more than one dimension in a tabular form,
such as rows and columns. The multidimensional array support two or three dimensional in VB.NET.
Declaration of Multidimensional Array
Declaration of two-dimensional array
Dim twoDimenArray As Integer( , ) = New Integer(3, 2) {}
Or Dim arr(5, 3) As Integer
Representation of Three Dimensional array
Dim arrThree(2, 4, 3) As Integer
Or Dim arr1 As Integer( , , ) = New Integer(5, 5, 5) { }
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)
C#.NET SHREE MEDHA DEGREE COLLEGE, BALLARI
Dept of Computer Science From the desk of Ms. Vinutha H M M.Sc. (CS)