Database web hosting - 148 CHAPTER 5 STORING AND RETRIEVING APPLICATION
148 CHAPTER 5 STORING AND RETRIEVING APPLICATION DATA Retrieving Data from Arrays Once an array is populated with data, you can refer to any element in the array by specifying its index position. For example, the following statement retrieves the value assigned to the third element in the strNamesArray and assigns it to a variable. Dim strCurrentName As String StrCurrentName = strNamesArray(2) Rather than accessing individual array elements one at a time, you can set up a loop that enables you to programmatically process every element in an array. In this way, you can process an array containing dozens, hundreds, or many thousands of elements using a relatively small amount of code, as the following shows. Dim strNamesArray(4) As String Dim intCounter As Integer strNamesArray(0) = “Molly” strNamesArray(1) = “William” strNamesArray(2) = “Alexander” strNamesArray(3) = “Jerry” strNamesArray(4) = “Mary” For intCounter = 0 To Ubound(strNamesArray) MsgBox(”Now processing ” + strNamesArray (intCounter)) Next intCounter When this example runs, the For Next loop will read and display the name of each array element one at a time in a series of pop-up dialog windows. Note the use of the Ubound func tion in the previous example. This function returns the index number of the last element stored in the specified array, which in this example would be 4. The Ubound function is designed to help set up loops that process arrays whose length may not be known in advance. Loops pro vide the capability to iterate through collections of data-like arrays. More information on the For Next loop is available in Chapter 7. Changing the Size of Your Arrays You may not always know in advance exactly how many elements you need to store in an array. In these circumstances, declaring your array as a null array (or an empty array) is best. You can do this either by specifying a 1 as the array s index number or by simply not specifying anything at all. For example, both of the following statements are functionally equivalent. Dim strNamesArray(-1) Dim strNamesArray() When defined in this manner, you can wait until later in your application to redefine the array when you know how big it needs to be. You do this using the Redim statement, as the fol lowing shows. Redim strNamesArray(9)
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.