Chapter 17 Passing Parameters

When programs become more complex, it is wise to break the program up into modules called Procedures and/or Functions. These modules then perform a specific task and may be used many times within the program. However, data must be passed to and from the module. One method of giving access to the module is to declare Public Variables. However, this can cause problems or "side effects" especially in large programs. A better way to do this is to pass values to and from the modules as parameters.

Visual Basic allows data to be passed into a module by value such that  the value of the original data is not changed or passed into and out of a module by reference such that  the value of the original data is changed.

An example of this is when two values need to be swapped. The following program will swap the content of two text boxes.

Name Swap problem

You are to write a Visual Basic program that will allow a user to enter two words - one in each of two text boxes. When the swap button is clicked, the words in the text boxes are swapped . The program requires that you use a procedure called swap.

Planning

A procedure called swap is to be created that accepts two strings as parameters by reference. The procedure will swap the strings over and pass them back out in the alternate order. To do this, a temporary variable is used.

Text boxes should be called txt1 and txt2; swap button cmdSwap

Pseudcode

main
stringA = text1
stringB = text2
call swap (stringA, stringB)
display StringA
display StringB
end


Procedure swap(first, second)
  
    Set temp To first
    Set first To second
    Set second To temp

Coding
NOTE: VB.Net handles procedures / parameter passing in a somewhat different fashion than VB6 and its precdecessors. Hence you need to follow these instructions carefully. Ensure that you have saved your work before proceding!

The swap procedure will be created in a module. This frees it from being associated with the Form (so if we had another form then it would still be able to use the procedure)

From the toolbar select Project - Add Module - Module. Call it MySwap. A code window will open. Press enter.

Copy the following code and paste it into the space.

Public Sub swapprocedure(ByRef firstword As String, ByRef secondword As String)
Dim temp As String
temp = firstword
firstword = secondword
secondword = temp
End Sub

Return to your Form and double click the swap button to open the cmdSwap code window. Paste in the following code.

Dim StringA, StringB As String

StringA = txt1.Text
StringB = txt2.Text
'now call the swap procedure - note CALL not required
swapprocedure(StringA, StringB)

txt1.Text = StringA
txt2.Text = StringB

 

Explanation

When the swap button is clicked, the values in the text boxes are copied into the StringA and StringB variables. These are then passed by reference into the swap module as the variables firstword and secondword. The value of first is copied into the temp variable. The value of second is copied into first and then the value of temp is copied into second. As these are passed by reference, when the procedure ends, the values in the main program have been changed. it is just a matter of replacing the original values in the text boxes with the updated values.

Exercise 17

Problem 1

Modify the  procedure that will accept two values and return them in the order of largest to smallest.  

Problem 2

Write a procedure that will accept the time of date in dd/mm/yyyy format and return it in three different fields of dd Month and yyyy. ie 12/03/2001 would return 12  March 2000 as three separate fields.

Problem 3 (Very difficult)

Extend problem 2 to return the date on the form Day-DD Month and yyyy. ie 12/03/2000 would return Sunday-12 March 2001 as three separate fields. This program will require more than one procedure. 

Problem 4

Write a set of procedures that will allow an array of numbers to be sorted using the bubble sort method. 


Original Author Mike Leishman

Updated for VB.Net FDS 24/4/07