Introduction to Arrays

An array is a record that holds multiple values of the same type.  For example, an array named strMonth might hold the 12 months of the year in string format.  You might think of an array like a collapsible folder in windows.  The folder identifies the type of data inside, and when you open it, you find lots of different data, but all of the same type.



Now I have two “variables,” each holding a variety of data records.  Here’s some additional information about these records:  each one is numbered, in sequential order.  In Visual Studio, we use “zero-based arrays.”  This simply means that the first record in an array is numbered “0.”  So, an array of all the months of the year would be numbered from 0 – 11, to account for all 12 months.

Remember back to the list boxes we used in the pizza project?  We could identify an item and remove it from the list based on its index.  Well, arrays work the same way.  I can pick out a record from an array using an index.  So, if I ask to see index 5 from strMonths, and index 5 from strHoursWorked, I’ll get these two results:  “June,” and “165.”

As you might imagine, this is how a database works.  Each type of information is stored in an array and each index number in the array corresponds to the same item.  So, if I want the name and address and marital status of a particular customer, I just need to query (ask) the appropriate arrays to return the records at the appropriate index position

Arrays of the same length in which each element in an array is related to an element in the second array at the same index value are called “parallel arrays.”

Now that you have an idea about what an array is and why it’s so cool, you can read in the book about how to declare an array and how to populate an array with data records.

Once you have records in your array, you can access them by calling the array and index position.  For example, if I want to get the 4th month from the strMonth array and display it in a label, I might enter code like this:

     lblMonth.Text = strMonth(index)

But how does the program know what index to look for?  Well, we need to specify it.  Usually we do this with user input.  To make this easy, we can declare a variable, like

                Dim intIndex as integer

Then we can set the value of intIndex with user input such as

                intIndex = Val(txtIndex.Text)

where txtIndex.Text is a textbox asking for the user to “Enter a number from 0 to 11,” or “Enter a number of a month of the year.”

Now, strMonth(intIndex) is evaluated as strMonth(Whatever number the user entered, let’s say 4)

strMonth(4) tells the program to identify the record at index position 4 in the array strMonth, 

All arrays have methods that allow for ease of identifying the length and boundaries.  For example:

                strMonth.length will return the number of records in an array

                strMonth.GetLowerBound and strMonth.GetUpperBound will return the lowest and highest index numbers in an array

You can also use loops with arrays to iterate through and process the records in an array one at a time.  More on that in another post.