Learn array in vb.net / PDF
Learn array in vb.net / PDF
1
OVERVIEW
OF ARRAYS
1.1
Definition
1.2
Purpose
of the Arrays
1.3
Array
Elements
1.4
Array
Indexes
1.5
Changing
the Base Index of an Array
1.6
Array
Dimension
1.7 Declaring
Arrays
2
DEMONSTRATING
ARRAYS USING EXAMPLE
1
OVERVIEW
OF ARRAYS
The ability to work with
arrays is important in any programming language. VB.NET offers a simple way of
grouping data into the array structures similar to other languages.
1.1
Definition
Arrays are programming
constructs that store data and allow us to access them by numeric index
or subscript. All arrays in VB as zero based, meaning, the
index of the first element is zero and they are numbered sequentially. Arrays
have an upper bound and a lower bound, which simply refer to the starting index
and the ending index of a given array structure.
1.2
Purpose
of the Arrays
§
Arrays are generally used for
storing similar types of values or objects. They allow grouping variables
together and allow referring to them by using an index.
§
Arrays help us create shorter
and simpler code in many situations.
§
Additionally, the data in the
array can be sorted.
§
You can loop through an array
to determine and to modify the values stored in the array.
1.3
Array
Elements
What is an array element? When you define
any type of array, you are actually defining a set of variables of the same
data type that can be referenced by a single name. Each one of those item is
called an element of the array. For instance in the following example: 1
is an element of the array arrNumbers().
Dim arrNumbers() As Integer
arrNumbers
= New Integer() {0,1,2,3,4}
|
1.4
Array
Indexes
To distinguish one element of an array
from the others, each element of an array is associated with an index number.
That index number is used to reference each distinct element of the array. Note
that the index must be positive.
1.5
Changing
the Base Index of an Array
At times, starting an array with an index
value of 0 (zero) seems counter-intuitive. For example, do users really think
of January as zeroth month of the year, or do they think of it is the first
month of the year? Generally speaking, January is considered as the first month
of the year. Therefore, it would be nice to define the array in a way that is
consistent with the way you expect to use the array. It would make more sense
to have January have an index value of 1 than an index value of 0. There are
several ways to accomplish this. Let’s use an example.
Dim sport(6) As String
sport(0) =
"Saturday"
sport(1) =
"Sunday"
sport(2) =
"Monday"
sport(3) =
"Tuesday"
sport(4) =
"Wednesday"
sport(5) =
"Thursday"
sport(6) =
"Friday"
|
One way to handle the above problem is to
start by assigning nothing to the array at location 0, that is:
Dim sport(7) As String
sport(0) = ""
sport(1) =
"Saturday"
sport(2) =
"Sunday"
sport(3) =
"Monday"
|
1.6
Array
Dimension
An array can be one-dimension
(linear arrays) or more than one (multidimensional arrays). The
dimensionality of an array refers to the number of subscripts used to identify
an individual element.
1.7 Declaring
Arrays
There are two ways of
initializing the arrays: to declare and initialize them in one statement, or to
declare an array and choose to initialize it later.
When declaring and
initializing arrays in the same statement, you must specify the type of the
array elements and the number of the elements the array will hold. You must
specify the number of array elements by indicating the upper bound of the
array; the upper bound is the number that specifies the index of the last
element in the array. For instance, to declare & initialize an array of 5
integers, with indexes ranging from 0 to 4, we can write:
Dim arrNumbers(4) As Integer
|
Another way to declare
and initialize arrays is to perform these operations in two separate steps. If
you declare an array without specifying a number of elements on one line, you
have to provide the values for each item of the array when you initialize it.
The initial values are provided enclosed in the {} braces, using a comma as a
separator.
To declare and initialize an array in two
separate steps, we can write:
Dim arrNumbers() As Integer
arrNumbers = New Integer() {0,1,2,3,4}
|
2
DEMONSTRATING
ARRAYS USING EXAMPLE
Example
1:
Create a form with one button so that if you click the button, the value stored in array ..
Learn array in vb.net / PDF
0 commentaires: