VB.net project
VB.net project
NET Framework :-
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.
Note: The .NET Framework is not only a language, but it is also a software and
language neutral platform.
Why .Net ?
1. Cross-Platform Development
Develop applications that run on Windows, Linux, and macOS using .NET
Core and .NET 5+.
Create mobile applications for iOS and Android with Xamarin or .NET
MAUI using a shared codebase.
Known for high performance with optimizations like just-in-time (JIT) and
ahead-of-time (AOT) compilation.
Ideal for building scalable applications, from small projects to enterprise-
grade systems.
3. Versatility
5. Developer Productivity
6. Security
8. Cost-Effectiveness
Open Source: .NET Core and .NET 5+ are open source, reducing licensing
costs.
Integrates seamlessly with Azure, which can save development and
deployment time.
JIT –
Working of JIT Compiler: The JIT compiler is required to speed up the code
execution and provide support for multiple platforms. Its working is given as
follows:
The JIT compiler converts the Microsoft Intermediate Language(MSIL) or
Common Intermediate Language(CIL) into the machine code. This is done before
the MSIL or CIL can be executed. The MSIL is converted into machine code on a
requirement basis i.e. the JIT compiler compiles the MSIL or CIL as required rather
than the whole of it. The compiled MSIL or CIL is stored so that it is available for
subsequent calls if required.
Types of Just-In-Time Compiler: There are 3 types of JIT compilers which are as
follows:
Pre-JIT Compiler: All the source code is compiled into the machine code at
the same time in a single compilation cycle using the Pre-JIT Compiler. This
compilation process is performed at application deployment time. And this
compiler is always implemented in the Ngen.exe (Native Image Generator).
Normal JIT Compiler: The source code methods that are required at run-
time are compiled into machine code the first time they are called by the
Normal JIT Compiler. After that, they are stored in the cache and used
whenever they are called again.
Econo JIT Compiler: The source code methods that are required at run-time
are compiled into machine code by the Econo JIT Compiler. After these
methods are not required anymore, they are removed. This JIT compiler is
obsolete starting from dotnet 2.0
Advantages of JIT Compiler:
The JIT compiler requires less memory usage as only the methods that are
required at run-time are compiled into machine code by the JIT Compiler.
Page faults are reduced by using the JIT compiler as the methods required
together are most probably in the same memory page.
Code optimization based on statistical analysis can be performed by the JIT
compiler while the code is running.
The JIT compiler requires more startup time while the application is executed
initially.
The cache memory is heavily used by the JIT compiler to store the source
code methods that are required at run-time.
It specifies a standard that represent what type of data and value can be defined and
managed in computer memory at runtime. A CTS ensures that programming data
defined in various languages should beinteract with each other to share information.
For example, in C# we define data type as int, while in VB.NET we define integer
as a data type.
The base class library has a rich collection of libraries features and functions that
help to implement many programming languages in the .NET Framework, such as
C #, F #, Visual C ++, and more. Furthermore, BCL divides into two parts:
It is a subset of common type system (CTS) that defines a set of rules and
regulations which should be followed by every language that comes under the .net
framework. In other words, a CLS language should be cross-language integration or
interoperability. For example, in C# and VB.NET language, the C# language
terminate each statement with semicolon, whereas in VB.NET it is not end with
semicolon, and when these statements execute in .NET Framework, it provides a
common platform to interact and share information with each other.
A .NET assembly is the main building block of the .NET Framework. It is a small
unit of code that contains a logical compiled code in the Common Language
infrastructure (CLI), which is used for deployment, security and versioning. It
defines in two parts (process) DLL and library (exe) assemblies. When the .NET
program is compiled, it generates a metadata with Microsoft Intermediate
Language, which is stored in a file called Assembly.
It provides the various system functionality in the .NET Framework, that includes
classes, interfaces and data types, etc. to create multiple functions and different
types of application such as desktop, web, mobile application, etc. In other words, it
can be defined as, it provides a base on which various applications, controls and
components are built in .NET Framework.
Saves development time: You don't need to write everything from scratch,
use pre-built functionalities.
Increases code consistency: Standardizes coding practices across projects.
Improves code reliability: Tested code from Microsoft reduces bugs.
Namespaces
Benefits of Namespaces:
While VB.NET doesn't have different "types" of namespaces in a strict sense, there
are ways to categorize them based on their source:
Ex :-
Namespace SpecialSpace
Namespace System
Class abc
Function getValue() As System.Int32
Dim n As System.Int32
Return n
End Function
End Class
End Namespace
End Namespace
VB.NET Data Type
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.
User-Defined (structure) A user-defined data type Each member of the structure has its own
depends on the data type and limits independent of the
implementing platform other members' ranges.
Example :-
Module Data_type
Sub Main()
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 NSU")
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
VB.NET Operators
Example :-
Sub Main()
'Declare a, b And c as integer Data Type()
Dim a, b, c As Integer
a = 17
b=4
' Use of + Operator
c=a+b
Console.WriteLine(" Sum of a + b is {0}", c)
'Use of - Operator
c=a-b
Console.WriteLine(" Subtraction of a - b is {0}", c)
'Use of * Operator
c=a*b
Console.WriteLine(" Multiplication of a * b is {0}", c)
'Use of / Operator
d=a/b
Console.WriteLine(" Division of a / b is {0}", d)
'Use of \ Operator
c=a\b
Console.WriteLine(" Similar to division Operator (return only integer value) of
a - b is {0}", c)
'Use of Mod Operator
c = a Mod b
Console.WriteLine(" Modulus of a Mod b is {0}", c)
'Use of ^ Operator
c=a^b
Console.WriteLine(" Power of a ^ b is {0}", c)
End Sub
Comparison Operators
As the name suggests, the Comparison Operator is used to compare the value of
two variables or operands for the various condition such as greater, less than or
equal, etc. and returns a Boolean value either true or false based on the condition.
< It is a less than symbol which (A < B); if the condition is true,
checks whether the value of the returns TRUE else FALSE
left operand is less than the value
of the right operand; If the
condition is true, it returns TRUE;
otherwise, it shows FALSE value.
>= It is greater than equal to which A >= B
checks two conditions whether
the first operand is greater than
or equal to the second operand; if
yes, it returns TRUE; otherwise, it
shows False.
<= This symbol represents less than A <= B
equal to which determines the
first operand is less than or equal
to the second operand, and if the
condition is true, it returns TRUE;
otherwise, it shows FALSE.
Is The Is Operator is used to result = obj1 Is obj2
validate whether the two objects
reference the same variable or
object; If the test is true, it returns
True; otherwise, the result is
False. In short, it checks the
equality of the objects. An Is
Operator is also used to
determine whether the object
refers to a valid object.
IsNot The IsNot Operator is similar to Is Result = obj1 IsNot obj2
Operator, except that the two
object references the different
object; if yes, the result is True;
otherwise, the result is False.
Like The Like Operator is used to result = string Like the pattern,
check the pattern expression of the pattern represents the
string variable; And if the pattern series of characters used by
matched, the result is True; Like Operator.
otherwise, it returns False.
Example :-
Sub Main()
'declaration of Integer, Object and String Data Type variables
Dim x As Integer = 5
Dim y As Integer = 10
Dim Result, obj, obj2 As Object
Dim str, str2 As String
str = "Apple12345"
str2 = "Apple12345"
obj = 10
obj2 = 20
Console.WriteLine(" Program of Comparison Operator")
'Use of > Operator
Console.WriteLine(" Output of x > y is {0}", x > y)
'Use of < Operator
Console.WriteLine(" Output of x < y is {0}", x < y)
'Use of = Operator
Console.WriteLine(" Output of x = y is {0}", x = y)
'Use of <> Operator
Console.WriteLine(" Output of x <> y is {0}", x <> y)
'Use of >= Operator
Console.WriteLine(" Output of x >= y is {0}", x >= y)
'Use of <= Operator
Console.WriteLine(" Output of x <= y is {0}", x <= y)
'Use of Is Operator
Result = obj Is obj2
Console.WriteLine(" Output of obj Is obj2 is {0}", Result)
'Use of Is Operator
Result = obj IsNot obj2
Console.WriteLine(" Output of obj IsNot obj2 is {0}", Result)
'Use of Like Operator
Result = str Like str2
Console.WriteLine(" Output of str Like str2 is {0}", Result)
End Sub
Logical and Bitwise Operators
The logical and bitwise Operators work with Boolean (true or false)
conditions, and if the conditions become true, it returns a Boolean value. The
following are the logical and bitwise Operators used to perform the various
logical operations such as And, Or, Not, etc. on the operands (variables).
Suppose there are two operand A and B, where A is True, and B is False.
Sub Main()
Dim A As Boolean = True
Dim B As Boolean = False
Dim c, d As Integer
c = 10
d = 20
'Use of And Operator
If A And B Then
Console.WriteLine(" Operands A And B are True")
End If
'Use of Or Operator
If A Or B Then
Console.WriteLine(" Operands A Or B are True")
End If
'Use of Xor Operator
If A Xor B Then
Console.WriteLine(" Operands A Xor B is True")
End If
'Use of And Operator
If c And d Then
Console.WriteLine(" Operands c And d is True")
End If
'Use of Or Operator
If c Or d Then
Console.WriteLine(" Operands c Or d is True")
End If
'Use of AndAlso Operator
If A AndAlso B Then
Console.WriteLine(" Operand A AndAlso B is True")
End If
'Use of OrElse Operator
If A OrElse B Then
Console.WriteLine(" Operand A OrElse B is True")
End If
'Use of Not Operator
If Not (A And B) Then
Console.WriteLine(" Output of Not (A And B) is True")
End If
End Sub
Operator Description
Example ;-
Sub Main()
Dim x, y, z As Integer
x = 12
y = 25
Dim a, b As Double
a = 5 ' a = 5(00000101)
b = 9 ' b = 9(00001001)
' Use of And Operator
z = x And y
Console.WriteLine(" BitShift Operator x And y is {0}", z)
'Use of Or Operator
z = x Or y
Console.WriteLine(" BitShift Operator x Or y is {0}", z)
z = x Xor y
Console.WriteLine(" BitShift Operator x Xor y is {0}", z)
z = Not y
Console.WriteLine(" BitShift Operator Not y is {0}", z)
'Use of << Left-Shift Operator
' Output is 00001010
Console.WriteLine(" Bitwise Left Shift Operator - a<<1 = {0}", a << 1)
'Output is 00010010
Console.WriteLine(" Bitwise Left Shift Operator - b<<1 = {0}", b << 1)
'Use of >> Right-Shift Operator
'Output is 00000010
Console.WriteLine(" Bitwise Right Shift Operator - a>>1 = {0}", a << 1)
'Output is 00000100
Console.WriteLine(" Bitwise Right Shift Operator - b>>1 = {0}", a << 1)
End Sub
Assignment Operators
The Assignment Operators are used to assign the value to variables in
VB.NET.
Sub Main()
'Declare variable and b As Integer
Dim A As Integer = 5
Dim B As Integer
Dim Str, name As String
name = "come"
Str = "Wel"
'Use of = Operator
B=A
Console.WriteLine(" Assign value A to B is {0}", B)
'Use of += Operator
B += A
Console.WriteLine(" Output of B += A is {0}", B)
'Use of -= Operator
B -= A
Console.WriteLine(" Output of B -= A is {0}", B)
'Use of *= Operator
B *= A
Console.WriteLine(" Output of B *= A is {0}", B)
'Use of /= Operator
B /= A
Console.WriteLine(" Output of B /= A is {0}", B)
'Use of = Operator
B \= A
Console.WriteLine(" Output of B \= A is {0}", B)
'Use of ^= Operator
B ^= A
Console.WriteLine(" Output of B ^= A is {0}", B)
'Use of &= Operator
Str &= name
Console.WriteLine(" Output of Str &= name is {0}", Str)
Console.WriteLine(" Press any key to exit...")
Console.ReadKey()
End Sub
Concatenation Operators
In VB.NET, there are two concatenation Operators to bind the operands:
Example :-
Sub Main()
Dim str As String = "Wel"
Dim str2 As String = "come"
Dim str3 As String = " "
Dim str4 As String = "to JavatPoint"
Dim result As String
Dim result2 As String
result = str & str2
Console.WriteLine(" Result = str & str2 gives = {0}", result)
result2 = str + str2 + str3 + str4
Console.WriteLine(" Result = str + str2 + str3 +str4 gives = {0}",
result2.ToString)
End Sub
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. In the control statement, we will use if- Then, if
Then Else, if Then ElseIf and the Select case statement.
We can define more than one condition to be evaluated by the program with
statements. If the defined condition is true, the statement or block executes
according to the condition, and if the condition is false, another statement is
executed.
The following figure shows a common format of the decision control statements
to validate and execute a statement:
The above diagram shows that if the defined condition is true, statement_1 will be executed, and if the
condition is false, statement_2 will be executed.
If-Then Statement
If-Then Else Statement
If-Then ElseIf Statement
Select Case Statement
Nested Select Case Statements
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 : Write a simple program to find the greater number in VB.NET.
Sub Main()
Dim a As Integer = 5
Dim b As Integer = 8
If a > b Then
Console.Writeline("A is greater than b ")
End If
End Sub
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 :- Write a program to check weather the number is even or odd .
Sub Main()
Dim num As Integer
Console.WriteLine("Enter the Number")
num = Console.ReadLine() 'read data from console
If (num Mod 2 = 0) Then ' if condition is true, print the if statement
Console.WriteLine("It is an even number")
Else 'otherwise, Else statement is executed.
Console.WriteLine("It is an odd number")
End If
Console.WriteLine("press any key to exit...")
Consle.ReadKey()
End Sub
Syntax
If(condition 1)Then
' Executes when condition 1 is true
ElseIf( condition 2)Then
' Executes when condition 2 is true
ElseIf( boolean_expression 3)Then
' Executes when the condition 3 is true
Else
' executes the default statement when none of the above conditions is true.
End If
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.
Sub Main()
Dim var1 As Integer
Console.WriteLine(" Input the value of var1: ")
var1 = Console.ReadLine()
If var1 = 20 Then
'if condition is true then print the following statement'
Console.WriteLine(" Entered value is equal to 20")
ElseIf var1 < 50 Then
Console.WriteLine(" Entered value is less than 50")
ElseIf var1 >= 100 Then
Console.WriteLine(" Entered value is greater than 100")
Else
'if none of the above condition is satisfied, print the following statement
Console.WriteLine(" Value is not matched with above condition")
End If
Console.WriteLine(" You have entered : {0}", var1)
End Sub
In VB.NET, the Select Case statement is a collection of multiple case statements, which
allows executing a single case statement from the list of statements. A selected case
statement uses a variable to test for equality against multiple cases or statements in a
program. If the variable is matched with any test cases, that statement will be executed.
And if the condition is not matched with any cases, it executes the default statement.
Using the select case statement in VB.NET programming, you can replace the uses of
multiple If-Then-Else If statement from the program for better readability and easy to use.
Syntax
Following is the syntax of the Select Case statement in VB.NET, as follows:
Select Case [variable or expression]
Case value1 'defines the item or value that you want to match.
// Define a statement to execute
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
Case value2, value3
Statement2
Case Else
// define the default statement if none of the condition is true
End Select
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.
Sub Main()
'define a local variable.
Dim Days As String
Days = "Thurs"
Select Case Days
Case "Mon"
Console.WriteLine(" Today is Monday")
Case "Tue"
Console.WriteLine(" Today is Tuesday")
Case "Wed"
Console.WriteLine("Today is Wednesday")
Case "Thurs"
Console.WriteLine("Today is Thursday")
Case "Fri"
Console.WriteLine("Today is Friday")
Case "Sat"
Console.WriteLine("Today is Saturday")
Case "Sun"
Console.WriteLine("Today is Sunday")
Case Else
Console.WriteLine(" You have typed Something wrong")
End Select
Console.WriteLine("You have selected : {0}", Days)
Console.WriteLine("Press any key to exit...")
Console.ReadLine()
End Sub
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.
Types of Loops
There are five types of loops available in VB.NET:
Do While Loop
For Next Loop
For Each Loop
While End Loop
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 similar to 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
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.
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
End Sub
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
End Sub
Syntax
Do
// Statement of the outer loop
Do
// Statement of outer loop
While (condition -2)
// Statement of outer loop
While (condition -1)
Example 2: Write a simple program to use the Do While loop Statement in VB.NET.
Sub Main()
' Declare i and j as Integer variable
Dim i As Integer = 1
Do
' Outer loop statement
Console.WriteLine(" Execution of Outer loop is {0}", i & " times")
Dim j As Integer = 1
Do
'Inner loop statement
Console.WriteLine(" Execution of Inner loop is {0}", j)
j = j + 1 ' Increment Inner Counter variable by 1
Loop While j < 3
Console.WriteLine()
i = i + 1 ' Increment Outer Counter variable by 1
Loop While i < 4
Console.WriteLine(" Exit from the loop")
End Sub
Syntax
In the above flow chart, the first step is to initialize the variable name with the start value.
And then, the value of the variable will be compared to the end expression or value. If the
condition is true, the control enters the loop body and executes the statements. After that,
the value of a variable will be automatically incremented by the compiler. Upon
completion of each iteration, the current value of a variable will be again compared to the
end expression. If the condition is not true, the controlled exit from the loop.
Example 1. Write a simple program to print the number from 1 to 10 using the For Next loop.
Sub Main()
' It is a simple print statement,
Console.WriteLine(" The number starts from 1 to 10 ")
' 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
End Sub
Example :-
Sub Main()
Dim i, j As Integer
For i = 1 To 3
'Outer loop
Console.WriteLine(" Outer loop, i = {0}", i)
'Console.WriteLine()
'Inner loop
For j = 1 To 4
Console.WriteLine(" Inner loop, j = {0}", j)
Next
Console.WriteLine()
Next
End Sub
For Each Loop
In the VB.NET, For Each loop is used to iterate block of statements in an array or
collection objects. Using For Each loop, we can easily work with collection objects such
as lists, arrays, etc., to execute each element of an array or in a collection. And when
iteration through each element in the array or collection is complete, the control
transferred to the next statement to end the loop.
Syntax:
For Each var_name As [ DataType ] In Collection_Object
[ Statements to be executed]
Next
For Each loop is used to read each element from the collection object or an array. The
Data Type represents the type of the variable, and var_name is the name of the variable to
access elements from the array or collection object so that it can be used in the body of
For Each loop.
Sub Main()
'declare and initialize an array as integer
Dim An_array() As Integer = {1, 2, 3, 4, 5}
Dim i As Integer 'Declare i as Integer
For Each i In An_array
Console.WriteLine(" Value of i is {0}", i)
Next
End Sub
Syntax:
While [condition]
[ Statement to be executed ]
End While
Here, condition represents any Boolean condition, and if the logical condition is true,
the single or block of statements define inside the body of the while loop is executed.
Example: Write a simple program to print the number from 1 to 10 using while End
loop in VB.NET.
Sub Main()
'declare x as an integer variable
Dim x As Integer
x=1
' Use While End condition
While x <= 10
'If the condition is true, the statement will be executed.
Console.WriteLine(" Number {0}", x)
x = x + 1 ' Statement that change the value of the condition
End While
End Sub
Nested While End Loop
In VB.NET, when we write a While End loop inside the body of another While End
loop, it is called Nested While End loop.
Syntax
While (condition -1)
// body of the outer while loop
While (condition -2)
// body of inner while loop
End While
// body of the outer loop
End While
Example :-
Sub Main()
' Declare i and j as Integer variable
Dim i As Integer = 1
While i < 4
' Outer loop statement
Console.WriteLine(" Counter value of Outer loop is {0}", i)
Dim j As Integer = 1
While j < 3
'Inner loop statement
Console.WriteLine(" Counter value of Inner loop is {0}", j)
j = j + 1 ' Increment Inner Counter variable by 1
End While
Console.WriteLine() 'print space
i = i + 1 ' Increment Outer Counter variable by 1
End While
End Sub
Arrays
An array stores a fixed-size sequential collection of elements of the same type. An array
is used to store a collection of data, but it is often more useful to think of an array as a
collection of variables of the same type.
All arrays consist of contiguous memory locations. The lowest address corresponds to the
first element and the highest address to the last element.
You can also initialize the array elements while declaring the array. For example,
Dim intData() As Integer = {12, 16, 20, 24, 28, 32}
Dim names() As String = {"Karthik", "Sandhya", _
"Shivangi", "Ashwitha", "Somnath"}
Dim miscData() As Object = {"Hello World", 12d, 16ui, "A"c}
NOTE :-
In VB.NET when we create an array of size n then it means the total element store
in the array is n+1 , Because the staring index of an is 0 (zero) like others
programming language but the ending index is n in many programming language
the ending index is n-1 .
An array declared as Object can store elements of any data type (strings, numbers,
characters, etc.).
Example :-
1.
Sub Main()
Dim n(10) As Integer ' n is an array of 11 integers '
Dim i, j As Integer
' initialize elements of array n '
For i = 0 To 10
n(i) = i + 100 ' set element at location i to i + 100
Next i
' output each array element's value '
For j = 0 To 10
Console.WriteLine("Element({0}) = {1}", j, n(j))
Next j
Console.ReadKey()
End Sub
2.
Sub Main()
Dim n As Integer
Console.Write("enter size of an array :- ")
n = Console.ReadLine()
Dim arr(n) As Integer
For i = 0 To n
Console.Write("enter array at posion {0} - ", i)
arr(i) = Console.ReadLine()
Next
For i = 0 To arr.Length() - 1
Console.WriteLine("array at position {0} - {1}", i, arr(i))
Next
End sub
Code –
Public Class Form1
Dim a As Integer
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim b As Integer = TextBox2.Text
a = TextBox1.Text
Dim x As Integer = ListBox1.Items.Count()
If x <= a Then
ListBox1.Items.Add(b)
End If
End Sub
Public Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim arr(a) As Integer
For i = 0 To a
arr(i) = ListBox1.Items(i)
Next
For i = 0 To a
ListBox2.Items.Add(arr(i))
Next
End Sub
End Class
Dynamic Arrays
Dynamic arrays are arrays that can be dimensioned and re-dimensioned as par the need of
the program. You can declare a dynamic array using the ReDim statement.
The Preserve keyword helps to preserve the data in an existing array, when you
resize it.
arrayname is the name of the array to re-dimension.
subscripts specifies the new dimension.
Example –
Module arrayApl
Sub Main()
Dim marks() As Integer
ReDim marks(2)
marks(0) = 85
marks(1) = 75
marks(2) = 90
ReDim Preserve marks(10)
marks(3) = 80
marks(4) = 76
marks(5) = 92
marks(6) = 99
marks(7) = 79
marks(8) = 75
For i = 0 To 10
Console.WriteLine(i & vbTab & marks(i))
Next i
Console.ReadKey()
End Sub
End Module
Note –
while creating dynamic array we have to use preserve keyword if we don’t use
preserve keyword than redim create a new array of same name and delete all
existing element of an array that we store previously .
Multi-Dimensional Arrays
VB.Net allows multidimensional arrays. Multidimensional arrays are also called rectangular
arrays.
Example –
Sub Main()
' an array with 5 rows and 2 columns
Dim a(,) As Integer = {{0, 0}, {1, 2}, {2, 4}, {3, 6}, {4, 8}}
Dim i, j As Integer
' output each array element's value '
For i = 0 To 4
For j = 0 To 1
Console.WriteLine("a[{0},{1}] = {2}", i, j, a(i, j))
Next j
Next i
Console.ReadKey()
End Sub