Changes

Jump to: navigation, search

Visual Basic Arrays

1,410 bytes added, 15:10, 3 August 2007
What is a Visual Basic Array
== What is a Visual Basic Array ==
A Visual Basic array is an object that contains a specified number of itemsvariables of teh same type. Once you have grouped all the items variable values into the array you can then perform tasks like sorting the array items into alphabetical or numerical order, accessing and changing the value assigned to each array item, and passing the group of items as a parameter to a Visual Basic Function or Subroutine (see [[Visual Basic Modules and Procedures]]) by passing just the array object.
In this chapter we will look in detail at how to create and manipulate both single and multidimensional Visual Basic arrays.
 
Each value in an array is known as an ''array element''. The pointer to the location of a specific element in an array is called an ''index''.
 
== How to Declare a Visual Basic Array ==
 
An array is declared using the ''Dim'' keyword. The syntax is as follows:
 
'''Dim''' ''arrayname(size)'' As ''dataType''
 
The ''arrayname'' is the name by which the arrray will referenced in code. The ''size'' value in parentheses defines the maximum size of the array. It is important to note that arrays elements are numbered starting from 0, so an array of size 10 actually holds 11 elements (indexed as 0 though to 10).
 
Finally, the ''dataType'' value defines the type of variable the array will hold.
 
The following Visual Basic code excerpt declares an array capable of containing 6 String variables:
 
<pre>
Dim strColors(5) As String
</pre>
 
== Initializing a Visual Basic Array ==
 
As with other variable types, an array can be initialized during the declaration. This is achieved by encapsulating comma separated element values in braces ({}).
 
The following example initializes our colors array:
 
<pre>
Dim strColors(5) As String = {"Red", "Green", "Blue", "Indigo", "Violet", "Yellow" }
</pre>
 
Similarly, an Integer array would be initialized as follows:
 
<pre>
Dim intNumbers(4) As Integer = { 12, 43, 33, 45, 63 }
</pre>
 
== Assigning Values to Individual Array Elements ==

Navigation menu