Arrays_in_VB_Detailed_Notes
Arrays_in_VB_Detailed_Notes
1. Introduction
In programming, an array is a data structure that can store a fixed-size sequence of elements of the same
data type.
Instead of declaring multiple variables, arrays allow us to store a collection of data under a single variable
name
Visual Basic (VB) supports arrays and provides several features for creating, accessing, and manipulating
arrays.
Arrays are particularly useful for handling large amounts of data, performing mathematical computations,
2. Types of Arrays in VB
a) Single-Dimensional Arrays
A single-dimensional array is the most basic form of array. It stores data in a linear format (one row).
Declaration Syntax:
Example:
Assigning values:
scores(0) = 90
scores(1) = 85
scores(2) = 78
scores(3) = 92
scores(4) = 88
Accessing values:
Using Loop:
For i As Integer = 0 To 4
Next
b) Multi-Dimensional Arrays
A multi-dimensional array stores data in the form of a table (rows and columns).
Declaration Syntax:
Example:
Assigning values:
matrix(0, 0) = 10
matrix(0, 1) = 20
matrix(0, 2) = 30
matrix(1, 0) = 40
matrix(1, 1) = 50
matrix(1, 2) = 60
For i As Integer = 0 To 1
For j As Integer = 0 To 2
Next
Console.WriteLine()
Next
c) Dynamic Arrays
A dynamic array is declared without a fixed size and can be resized at runtime using the ReDim statement.
Declaration:
ReDim arr(2)
arr(0) = "Apple"
arr(1) = "Banana"
arr(2) = "Mango"
arr(3) = "Orange"
arr(4) = "Grapes"
Example:
marks(0) = 78
marks(1) = 85
marks(2) = 90
marks(3) = 82
marks(4) = 76
For i As Integer = 0 To 4
total += marks(i)
Next
avg = total / 5
- Storing and processing large datasets (e.g., student marks, employee salaries)
6. Advantages of Arrays in VB
- Looping support: Can be easily manipulated using For or For Each loops.
7. Limitations of Arrays in VB
- Fixed size (for static arrays): Must define the size at the time of declaration.
9. Conclusion
Arrays in Visual Basic are a fundamental tool for efficient programming. They provide a structured way to
handle
multiple values of the same data type. Whether it's storing a list of names, processing numerical data,
or managing tables, arrays are essential for simplifying complex problems. Visual Basic enhances the use of
arrays
with features like dynamic resizing, looping, and built-in functions, making them powerful and flexible.