Arrays

General Array Processing – Part 2

Passing an Array to a Function or Sub

The following sample program demonstrates how to pass an array to a Sub or Function, so that the Sub or Function can process the data in the array. In the sample program, the Main procedure declares the following array:

Dim aintTemps(6) As Integer

The Main procedure prompts the user to enter 7 temperatures, which are stored in the array. After all the temperatures are entered, the Sub "PrintTemps" is called, using this syntax:

PrintTemps(aintTemps)

Note that in the syntax above, only the name of the array is specified; no parentheses or bounds are specified.

The header of the "PrintTemps" Sub looks like this:

Private Sub PrintTemps(ByVal paintTemps() As Integer)

Note that the Sub header must specify the array parameter with an empty set of parentheses.

To loop through the array contents, note that the UBound function is used in the For statement to determine how many elements need to be processed:

For intX = 0 To UBound(paintTemps)

Console.WriteLine(CStr(paintTemps(intX)))

Next

The program code and corresponding output is shown below:

 VB Code: Screen-shot of run: Module Module1       Public Sub Main()           Dim aintTemps(6) As Integer         Dim intX As Integer           For intX = 0 To 6             Console.Write("Enter temperature #" & (intX + 1) & ": ")             aintTemps(intX) = Val(Console.ReadLine())         Next           PrintTemps(aintTemps)           Console.WriteLine()         Console.WriteLine("Press Enter to close this window.")         Console.ReadLine()       End Sub       Private Sub PrintTemps(ByVal paintTemps() As Integer)           Dim intX As Integer           Console.WriteLine()         Console.WriteLine("You entered:")         Console.WriteLine()           For intX = 0 To UBound(paintTemps)             Console.WriteLine(CStr(paintTemps(intX)))         Next       End Sub   End Module

Passing a Structure Array to a Function or Sub

The rules for passing a Structure array to a Function or Sub follows the same principles as those for passing other types of arrays to a function or subroutine. Just make sure the proper user-defined Type is specified in the "As" clause of the parameter being passed in the Sub or Function header.

The sample program below is a modification of the sample program from the previous topic's section on "Loading a Structure Array from a File and Displaying Its Contents". In this version, the displaying of the structure array's contents takes place in a separate Sub called "PrintEmps". The call to PrintEmps passes the structure array as follows:

PrintEmps(maudtEmpRecord)

Note that the Sub header expects an array of type "EmployeeRecord" to be passed in:

Private Sub PrintEmps(ByVal paudtEmps() As EmployeeRecord)

The program code and corresponding output is shown below:

 VB Code: Screen-shot of run: Module Module1       Private Structure EmployeeRecord         Dim EmpName As String         Dim DeptNbr As Integer         Dim JobTitle As String         Dim HireDate As Date         Dim HrlyRate As Single     End Structure       Private maudtEmpRecord() As EmployeeRecord       Public Sub Main()           Dim strEmpFileName As String         Dim intEmpFileNbr As Integer           Dim intArrX As Integer           strEmpFileName = My.Application.Info.DirectoryPath & "\EMPLOYEE.DAT"         intEmpFileNbr = FreeFile()           FileOpen(intEmpFileNbr, strEmpFileName, OpenMode.Input)           intArrX = -1           Do Until EOF(intEmpFileNbr)             intArrX += 1             ReDim Preserve maudtEmpRecord(intArrX)             With maudtEmpRecord(intArrX)                 Input(intEmpFileNbr, .EmpName)                 Input(intEmpFileNbr, .DeptNbr)                 Input(intEmpFileNbr, .JobTitle)                 Input(intEmpFileNbr, .HireDate)                 Input(intEmpFileNbr, .HrlyRate)             End With         Loop           FileClose(intEmpFileNbr)           PrintEmps(maudtEmpRecord)           Console.WriteLine()         Console.WriteLine("Press Enter to close this window.")         Console.ReadLine()       End Sub       Private Sub PrintEmps(ByVal paudtEmps() As EmployeeRecord)           Dim intX As Integer           For intX = 0 To UBound(paudtEmps)             With maudtEmpRecord(intX)                 Console.WriteLine(.EmpName.PadRight(20) _                                 & .DeptNbr.ToString.PadRight(6) _                                 & .JobTitle.PadRight(20) _                                 & Format(.HireDate, "MM/dd/yyyy").PadRight(12) _                                 & Format(.HrlyRate, "Standard"))             End With         Next       End Sub   End Module

Using ParamArray

The ParamArray keyword is used in the Sub or Function header for procedures that will accept an arbitrary number of arguments of the same data type and process them as an array.

Consider the following Sub:

Private Sub SumThem(ByVal ParamArray paintNums() As Integer)

Dim intX As Integer

Dim intSum As Integer

For intX = 0 To UBound(paintNums)

intSum += paintNums(intX)

Next

Console.WriteLine("The sum is " & intSum)

End Sub

On the surface, the above Sub looks like a procedure which accepts an array of Integer values and sums them.  However, the presence of the keyword ParamArray means that this Sub can be called with any number of arguments. When you call this Sub, you would pass it not an array, but any number of individual values.

Following are some sample calls and the results that they would produce:

SumThem(1, 3, 5, 8)   ' SumThem will print 17

SumThem(3, 4, 2)      ' SumThem will print 9

SumThem(3, 7)         ' SumThem will print 10

The program code and corresponding output is shown below:

 VB Code: Screen-shot of run: Module Module1       Public Sub Main()           Dim intValueA As Integer         Dim intValueB As Integer           intValueA = 3         intValueB = 7           Console.WriteLine()         Console.WriteLine("*** ParamArray - Call #1 ***")         SumThem(1, 3, 5, 8)           Console.WriteLine()         Console.WriteLine("*** ParamArray - Call #2 ***")         SumThem(intValueA, 4, 2)           Console.WriteLine()         Console.WriteLine("*** ParamArray - Call #3 ***")         SumThem(intValueA, intValueB)           Console.WriteLine()         Console.WriteLine("Press Enter to close this window.")         Console.ReadLine()       End Sub       Private Sub SumThem(ByVal ParamArray paintNums() As Integer)           Dim intX As Integer         Dim intSum As Integer           For intX = 0 To UBound(paintNums)             intSum += paintNums(intX)         Next           Console.WriteLine("The sum is " & intSum)       End Sub   End Module

The For Each/Next Loop

There is a variation of the For/Next loop (the For Each/Next loop) that allows you to traverse a either the elements of an array or a set of objects in a collection. Objects and Collections will come into play a little later in your travels with VB; here we will look at how the For Each/Next loop can be used to process the elements of an array. The syntax of the For Each/Next loop is as follows:

For Each ElementVariable In ArrayName

' process the current ElementVariable

Next

The example below is a variation of the previous example, using For/Each in the SumThem routine:

 VB Code: Screen-shot of run: Module Module1       Public Sub Main()           Dim intValueA As Integer         Dim intValueB As Integer           intValueA = 3         intValueB = 7           Console.WriteLine()         Console.WriteLine("*** ParamArray - Call #1 ***")         SumThem(1, 3, 5, 8)           Console.WriteLine()         Console.WriteLine("*** ParamArray - Call #2 ***")         SumThem(intValueA, 4, 2)           Console.WriteLine()         Console.WriteLine("*** ParamArray - Call #3 ***")         SumThem(intValueA, intValueB)           Console.WriteLine()         Console.WriteLine("Press Enter to close this window.")         Console.ReadLine()       End Sub       Private Sub SumThem(ByVal ParamArray paintNums() As Integer)           Dim intNumX As Integer         Dim intSum As Integer           For Each intNumX In paintNums             intSum += intNumX         Next           Console.WriteLine("The sum is " & intSum)       End Sub   End Module