Arrays in Visual basic

Discussion in 'Visual Basic [VB]' started by Sanskruti, Mar 27, 2007.

  1. Sanskruti

    Sanskruti New Member

    Joined:
    Jan 7, 2007
    Messages:
    108
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Consultant
    Location:
    Mumbai, India
    By definition, an array is a list of variables, all with the same data type and name. When we work with a single item, we only need to use one variable. However, if we have a list of items which are of similar type to deal with, we need to declare an array of variables instead of using a variable for each item.

    For example, if we need to enter one hundred names, instead of declaring one hundred different variables, we need to declare only one array. We differentiate each item in the array by using subscript, the index value of each item.

    Declaring arrays



    We could use Public or Dim statement to declare an array just as the way we declare a single variable. The Public statement declares an array that can be used throughout an application while the Dim statement declare an array that could be used only in a local procedure.

    The general format to declare an array is as follows:

    Dim arrayName(subs) as dataType
    where subs indicates the last subscript in the array.

    Dim CustName(10) as String
    will declare an array that consists of 10 elements if the statement Option Base 1 appear in the declaration area, starting from CustName(1) to CustName(10). Otherwise, there will be 11 elements in the array starting from CusName(0) through to CusName(10)

    Dim Count(100 to 500) as Integer declares an array that consists of the first element starting from Count(100) and ends at Count(500)

    An Example

    Code:
    Dim CustName(10) As String 
    Dim num As Integer 
    Private Sub addName() 
    	For num = 1 To 10 
    		CustName(num) = InputBox("Enter the Customer name", "Enter Name", "", 1500, 4500) 
    		If CustName(num) <> "" Then 
    			Form1.Print CustName(num) 
    		Else 
    			End 
    		End If 
    	Next 
    End Sub 
      
    Private Sub Exit_Click() //exit command button
    	End 
    End Sub 
    
    Private Sub Start_Click() //start command button
    	Form1.Cls 
    	addName 
    End Sub 
    The above program accepts data entry through an input box and displays the entries in the form itself. As you can see, this program will only allow a user to enter 10 names each time he click on the start button.

    Array Limitations



    The only data limitation of arrays is that all the elements of the array must be of the same data type. If this limitation is a problem to you, a collection might be a better solution than an array. Of course, when working with an array of variants, each element of the array can hold a different data type value. However, the extra memory and processing requirements of using variants in an array limit this approach.

    Declaring a variable-size array requires syntax similar to fixed-length arrays.

    Dim ArrayName ( ) [As DataType]

    In his case, the UpperBound and LowerBound are purposely omitted. Visual Basic recognizes this declaration as a variable-size array and expects you to eventually establish the array size with the Redim statement:

    Redim ArrayName ([LowerBound to [UpperBound]])

    As soon as the Redim statement is applied, Visual Basic allocates as much memory as required to contain the array. You can Redim an array multiple times, dynamically altering the amount of memory occupied by the array.

    The bounds of an array can be determined with the UBound (upper bound) and LBound (lower bound) functions. The syntax of these functions is as follows:

    UBound (ArrayName)
    LBound (ArrayName)

    Because the bound values of arrays are long integers, both of these functions return long values.

    You might use fixed and dynamic arrays in an application. Notice how the dynamic arrays are set to specific sizes by the Redim statement before they are used. Notice also that, because the Redim statements do not include the LowerBound specifier, each of the dynamic arrays begins at index 0.

    Code:
    Sub ArraryDemo ( )
         Dim aintInt1 (1 to 10) ‘Fixed array of integers
         Dim astrStr1 (1 to 10) ‘Fixed array of strings
    
         Dim aintInt2 ( ) ‘Dynamic array of integers
         Dim astrStr2 ( ) ‘Dynamic array of strings
    
         Dim i As Integer
    
         ReDim aintInt2 (5) ‘Now a fixed array of integers
         ReDim astrStr2 (5) ‘Now a fixed array of strings
    
         For i = 1 To 10
              aintInt1 (i) = i * 100
              astrStr1 (i) = Format (i * 100, “0000”)
              MsgBox i & “ “aintInt1 (i) & “ ” & astrStr1(i)
         Next i
    
         For i = 0 To 5
              aintInt2 (i) = i * 100
              astrStr2(i) = Format ( i * 100, “0000”)
              MsgBox i & “ “aintInt2 (i) & “ ” & astrStr2(i)
         Next i
    	
    End Sub
    The Redim statement discards the contents of an array if data is already present. Use the Preserve keyword when it is important to retain the contents of an array as it is redimensioned:

    ReDim Preserve astrInt2 (1 to 15)

    ReDim Preserve astrStr2 (1 to 15)

    The Preserve statement instructs Visual Basic to create a new array with the bounds specified in the Redim statement and then copy the contents of the original array into the new array.

    Multidimensional Arrays



    Now that you know what an array is, figuring out what a multidimensional array is will be a little easier. A multidimensional array is an array that looks like a table. If you have ever used Microsoft Excel, you would know . It can also be refered to an array of arrays. That is many arrays using the same name. Here is an example of a two-dimensional array.

    Static iArray(1 To 2, 1 To 3)

    All the elements list out are:

    iArray(1,1), iArray(1,2), iArray(1,3), iArray(2,1), iArray(2,2), iArray(2,3)

    Most of the time you will be using one dimensional arrays but some of the time it will be helpful to use 2 or even 3. More then 3 is not always a good idea because it tends to be hard to debug. The usefulness of VB will allow you to use as many as you want, each seperated by commas. Keep in mind the more you use, the more computer memory you use up. This can get real dangerous real fast. If you have some free time someday go ahead and make and 14-15 dimensional array. Make sure you close out and save everything else before hand. See how well your computer runs. Be prepared to have to restart.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice