## Visual Basic in 12 Easy Lessons velp08.htm

- Stop & Type
  - The Program's Description
  - The Program's Action
  - The External Module's Code
  - Descriptions
  - The Form Module's Code
  - Descriptions
  - Close the Application

#
Project 8

##
Modular Programming

###
Stop & Type

This lesson taught you how to break your programs into separate code components consisting of a form, subroutine procedures, function procedures, and external module files. The various pieces of the application work together to comprise the final program that the user sees when he or she runs the program.

By breaking your program into components, you can build upon past work. You can use subroutine procedures and function procedures that you've written before. You can define files of global constants just as Microsoft did by providing you with CONSTANT.TXT. When you reuse general-purpose code that you've written before, you save program development time and make debugging much simpler.

In this lesson, you saw the following:

- What subprograms are all about
- How subroutine procedures differ from function procedures
- When to use an external module file
- Why Visual Basic supports three kinds of variable scope
- How you can pass local values from one procedure to another

####
The Program's Description

Figure P8.1 shows the PROJECT8.MAK Project window. As you can see, the project consists of a form file containing the form, controls, and code pertaining to the form such as all the controls' event procedures. The project also contains the CONSTANT.TXT named constant file as well as a file named PROJECT8.BAS, an external module file designed with this application in mind.

Figure P8.1. Project 8's Project window contains three files.

The purpose of this project's application is to use general-purpose procedures, named constants, an external module, and the controls with which you've worked before to tie together this lesson's material.

####
The Program's Action

When you load and run PROJECT8.MAK, you'll first see an input box asking for a number from 1 to 100. A default value of 50 is used in case the user wants to press Enter to accept the default. The input box keeps displaying until the user enters a number between 1 and 100, or clicks Cancel or OK to get rid of the input box and accept the default value of 50.

After the user enters a valid number, Figure P8.2 shows the PROJECT8.MAK application's form that appears after the user enters a valid number. (The number entered by the user before Figure P8.2's form appeared was 47.)

Figure P8.2. Project 8's form window.

The program assumes that the user's number is a decimal base 10 number and that the number represents a Fahrenheit temperature reading. If you click the Base 16 option button, you'll see the hexadecimal equivalent to the user's number next to the Converted number: description. Click the Base 8 option button and the octal (base 8) representation of the user's number appears in the Bases frame.

The Temperatures frame changes the user's Fahrenheit temperature to Celsius and back again, depending on the chosen option button.

If the user clicks the Change Number command button, the user will be asked to enter a new number.

####
The External Module's Code

Listing P8.1 contains the complete code listing for the PROJECT8.BAS external module. As you can see, the module contains global named constants as well as a general-purpose function that accepts a Fahrenheit single-precision value and returns the Celsius equivalent. You can add this external module file to any application you write that needs to convert Fahrenheit temperatures to Celsius.

Listing P8.1. The external module file contains the application's named constants.

    1: Option Explicit

    2:

    3: ' Define global constants for the application

    4:

    5: ' Number base constants that match Index values

    6: Global Const BASE10 = 10

    7: Global Const BASE8 = 8

    8: Global Const BASE16 = 16

    9:

    10: ' Temperature constants that match Index values

    11: Global Const CELSIUS = 0

    12: Global Const FAHRENHEIT = 1

    13:

    14: Function GetCel (ByVal Faren As Integer)

    15: ' Assumes that a
    Fahrenheit temperature

    16: ' is passed. Returns that temp as Celsius

    17: GetCel = (Faren + 40) * (5 / 9) - 40

    18: End Function

####
Descriptions

1: Requires that all variables in the module be defined (or be arguments passed from elsewhere).

2: Blank lines help separate parts of code.

3: A remark helps explain the purpose of the (general) procedure.

4: Blank lines help separate parts of code.

5: A remark that explains how the subsequent named constants relate to Index subscripting properties of the two option button control arrays.

6: Define a name for the base option button control array item with 10 for an Index. Through the Property window, these three option buttons have Index property values of 10, 0, and 16.

> > 6: Instead of coding subscripts, the rest of the program uses named constants.

7: Define a name for the option button control array item with 8 for an Index.

8: Define a name for the option button control array item with 16 for an Index.

9: Blank lines help separate parts of code.

10: A remark helps explain the purpose of the subsequent code.

11: Define a name for the temperature option button control array item with 0 for an Index. Through the Property window, these two option buttons have Index property values of 0 and 1.

12: Define a name for the option button control array item with 1 for an Index.

13: A blank line separates the (general) procedure from the function procedure.

14: The general-purpose function procedure begins, receiving a single argument by value.

15: A remark helps explain the purpose of the function.

16: The remark continues on the next line.

17: Calculate the return value by assigning the converted Fahrenheit temperature to the function name.

> > 17: Always return a value when writing function procedures.

18: Terminate the function.

####
The Form Module's Code

Listing P8.2 contains the complete code listing for the PROJECT8.MAK file that uses the PROJECT8.BAS external module.

Listing P8.2. The form module's code controls the program flow.

    1: Sub Form_Load ()

    2: Call GetUserNum ' Stored in module file

    3: optbase(BASE10).Value = True

    4: optTemp(FAHRENHEIT).Value = True

    5:

    6: ' Trigger the event procedures

    7: ' for option button frames

    8: Call optBase_Click(BASE10)

    9:
    Call optTemp_Click(FAHRENHEIT)

    10: End Sub

    11:

    12: Sub GetUserNum ()

    13: ' A subroutine procedure that gets a

    14: ' number from 1 to 100 from the user

    15: ' and displays the number on the form

    16: Dim UserNum As Variant

    17: Do

    18: UserNum =
    InputBox("Enter a number from 1 to 100", "Ask", "50")

    19: If (UserNum = "") Then ' Check for Cancel

    20: ' Restore previous value

    21: UserNum = lblUserNum.Caption

    22: Exit Sub

    23: End If

    24: Loop While (UserNum
    < 1) Or (UserNum > 100)

    25: lblUserNum.Caption = UserNum

    26: End Sub

    27:

    28: Sub optBase_Click (Index As Integer)

    29: ' Determines what base the converted

    30: ' number displays in the base frame

    31: Select Case Index

    32: Case BASE10:

    33: '
    No change

    34: lblBaseOut.Caption = lblUserNum.Caption

    35: Case BASE16:

    36: lblBaseOut.Caption = Hex$(lblUserNum.Caption)

    37: Case BASE8:

    38: lblBaseOut.Caption = Oct$(lblUserNum.Caption)

    39: End Select

    40: End Sub

    41:

    42: Sub optTemp_Click (Index
    As Integer)

    43: ' Determines what temperature appears

    44: Select Case Index

    45: Case CELSIUS:

    46: lblTempOut.Caption = GetCel(Val(lblUserNum.Caption))

    47: Case FAHRENHEIT:

    48: lblTempOut.Caption = lblUserNum.Caption

    49: End Select

    50: End Sub

    51:

    52: Sub cmdChange_Click ()

    53: ' Asks the user once again for the number

    54: ' and calls appropriate click event

    55: ' procedures to update two frames

    56: Call GetUserNum

    57: optbase(BASE10).Value = True

    58: optTemp(FAHRENHEIT).Value = True

    59: Call
    optBase_Click(BASE10)

    60: Call optTemp_Click(FAHRENHEIT)

    61: End Sub

    62:

    63: Sub cmdExit_Click ()

    64: End

    65: End Sub

####
Descriptions

1: Define the procedure that executes right before the user sees the form.

2: Call the subroutine procedure that asks the user for a number between 1 and 100.

3: Activate the Base 10 option button. When the form finally appears, the Base 10 option button will be selected.

4: Activate the Fahrenheit option button. When the form finally appears, the Fahrenheit option button will be selected.

5: A blank line helps separate parts of a program.

6: A remark explains subsequent code.

7: The remark continues.

8: Trigger a click event procedure for the option buttons with the Bases frame. This event initializes the frame.

> > 8: Code can trigger event procedures.

9: Trigger a click event procedure for the option buttons with the Fahrenheit frame. This event initializes the frame.

10: Terminate the subroutine event procedure.

11: A blank line separates the Form_Load() procedure from the subroutine procedure that follows.

12: Define the subroutine procedure that requests a number from the user.

13: A remark explains subsequent code.

14: The remark continues.

15: The remark continues.

16: Define a variant variable that will capture the result of the input box.

17: Begin a loop that will ask the user for a number.

18: Display an input box and wait for the user's response.

19: Don't change the user's previously selected number (or the property defaults if this is the first time the user has been asked for a number) if the user clicks the Cancel command button.

> > 19: Always check for the user's Cancel command button selection.

20: A remark explains subsequent code.

21: Put the number back to its current value.

22: Terminate the subroutine procedure early.

23: Terminate the If that checked for the Cancel command button press.

24: Keep asking until the user enters a valid number within the range required.

25: The user has entered a valid number in the input box, so display the result.

26: Terminate the subroutine procedure.

27: A blank line separates the GetUserNum() procedure from the event procedure that follows.

28: Define the event procedure for the Bases option button control array.

29: A remark explains the purpose of the event procedure.

30: The remark continues on the next line.

31: The Index argument can be one of three values: 10, 16, or 8, as set in the Properties window for the three Index values. Test the value in the index to determine which option button to respond to.

32: If the user clicked the Base 10 option button...

33: A remark describes the code that follows.

34: The label inside the Bases frame matches the user's entered number because both are base 10.

35: If the user clicked the Base 16 option button...

36: Display the user's entered number as an hexadecimal string.

37: If the user clicked the Base 8 option button...

38: Display the user's entered number as an octal string.

39: Terminate the Select Case.

40: Terminate the event procedure.

41: A blank line separates the two event procedures.

42: Define the event procedure for the Temperatures option button control array.

43: A remark explains the purpose of the event procedure.

44: The Index argument can be one of two values: 0 or 1, as set in the Properties window for the two Index values. Test the value in the index to determine which option button to respond to.

45: If the user clicked the Celsius option button...

46: Display the user's entered number as a Celsius temperature.

47: If the user clicked the Fahrenheit option button...

48: The label inside the Temperature's frame matches the user's entered number because both are considered to be in Fahrenheit.

49: Terminate the Select Case statement.

50: Terminate the end procedure.

51: A blank line separates the event procedures.

52: Define the event procedure that executes when the user clicks Change Number.

53: A remark explains the purpose of the event procedure.

54: The remark continues on the next line.

55: The remark continues on the next line.

56: Call the subroutine procedure that gets a number from the user. No arguments are required.

> > 56: If the subroutine procedure requires no arguments, do not use parentheses.

57: Make the Base 10 Bases option button the default.

58: Make the Fahrenheit 10 Bases option button the default.

59: Trigger the event procedure that clicks the default Base 10 option button so that the Bases frame displays the user's number just entered.

60: Trigger the event procedure that clicks the default Fahrenheit option button so that the Temperatures frame displays the user's number just entered.

61: Terminate the event procedure.

62: A blank line separates the event procedures.

63: Define the event procedure for the Exit command button.

64: End the program when this event triggers.

65: Terminate the event procedure.

####
Close the Application

You can now exit the application and exit Visual Basic. The next lesson explains how to add disk file access to your applications so that you can store and retrieve long-term data in disk files.