Visual Basic 2005 for Programmers (2nd Edition)

4.4. Declaring a Method with a Parameter

In our car analogy in Section 4.2, we discussed the fact that pressing a car's gas pedal sends a message to the car to perform a taskmake the car go faster. But how fast should the car accelerate? The farther down you press the pedal, the faster the car accelerates. So the message to the car actually includes both the task to perform and additional information that helps the car perform the task. This additional information is known as a parameter (mentioned briefly in Section 4.3)the value of the parameter helps the car determine how fast to accelerate. Similarly, a method can require one or more parameters that represent additional information it needs to perform its task. A method call supplies valuescalled argumentsfor each of the method's parameters. For example, the method call to Console.WriteLine requires an argument that specifies the data to output in a Command Prompt window. Similarly, to deposit funds into a bank account, a Deposit method specifies a parameter that represents the deposit amount. When the Deposit method is called, the argument value representing the deposit amount is assigned to the method's parameter. The method then makes a deposit of that amount, increasing the balance in the bank account.

Our next example declares class GradeBook (Fig. 4.4) with a DisplayMessage method that displays the course name as part of the welcome message. (See the sample execution in Fig. 4.5.) The new DisplayMessage method requires a parameter that represents the course name to output (line 5).

Figure 4.4. Class declaration with one method that has a parameter.

1 ' Fig. 4.4: GradeBook.vb 2 ' Class declaration with a method that has a parameter. 3 Public Class GradeBook 4 ' display a welcome message to the GradeBook user 5 Public Sub DisplayMessage(ByVal courseName As String) 6 Console.WriteLine( _ 7 "Welcome to the grade book for " & vbCrLf & courseName & "!") 8 End Sub ' DisplayMessage 9 End Class ' GradeBook

Figure 4.5. Creating a GradeBook object and passing a String to its DisplayMessage method.

1 ' Fig. 4.5: GradeBookTest.vb 2 ' Create a GradeBook object and call its DisplayMessage method. 3 Module GradeBookTest 4 ' Main begins program execution 5 Sub Main() 6 ' initialize gradeBook to reference a new gradeBook object 7 Dim gradeBook As New GradeBook() 8 9 ' prompt for the course name 10 Console.WriteLine("Please enter the course name:") 11 12 ' read the course name 13 Dim nameOfCourse As String = Console.ReadLine() 14 15 Console.WriteLine() ' output a blank line 16 17 ' call gradeBook's DisplayMessage method 18 ' and pass nameOfCourse as an argument 19 gradeBook.DisplayMessage(nameOfCourse) 20 End Sub ' Main 21 End Module ' GradeBookTest

Please enter the course name: CS101 Introduction to Visual Basic Programming Welcome to the grade book for CS101 Introduction to Visual Basic Programming!

Before discussing the new features of class GradeBook, let us see how the new class is used from the Main method of module GradeBookTest (Fig. 4.5). Line 7 initializes variable gradeBook to reference a new GradeBook object. Note that empty parentheses are used in the object-creation expression (New GradeBook()), because no arguments need to be passed to create a GradeBook object. Line 10 prompts the user to enter a course name. Line 13 reads the name from the user and assigns it to the nameOfCourse variable, using method ReadLine to perform the input. The user types the course name and presses Enter to submit the course name to the program. Variable nameOfCourse is declared as a String (line 13). String is the type used for strings in Visual Basic, such as names, addresses, cities, states and product descriptions. Strings can consist of up to 2,147,483,648 (231) characters.

Line 19 calls gradeBook's DisplayMessage method. The argument nameOfCourse in parentheses is passed to method DisplayMessage so that it can perform its task. The value of variable nameOfCourse in Main (i.e., whatever course name the user types) becomes the value of method DisplayMessage's parameter courseName in line 5 of Fig. 4.4. When you execute this application, you will see that method DisplayMessage outputs a welcome message with the course name you type (Fig. 4.5).

More on Arguments and Parameters

When you declare a method, you must specify in the method's declaration whether the method requires data to perform its task. To do so, you place additional information in the method's parameter list, which is located in the parentheses that follow the method name. The parameter list may contain any number of parameters, including none at all. In Fig. 4.4, DisplayMessage's parameter list (line 5) declares that the method requires one parameter. Each parameter must specify a type and an identifier. In this case, the type String and the identifier courseName indicate that method DisplayMessage requires a String parameter named courseName in order to perform its task. At the time the method is called, the argument value in the call (in this case, the value of nameOfCourse in line 19 of Fig. 4.5) is assigned to the corresponding parameter in the method header (in this case, courseName in line 5 of Fig. 4.4). Then the method body uses the parameter courseName to access the value. Lines 67 of Fig. 4.4 display parameter courseName's value. The parameter variable's name (Fig. 4.4, line 5) can be the same or different from the argument variable's name (Fig. 4.5, line 19)there is no conflict if the names are identical, because as we will see in Chapter 7, Methods: A Deeper Look, these names appear in different "scopes."

Note that the parameter declaration in the parameter list looks similar to a variable declaration, but uses keyword ByVal instead of Dim. ByVal specifies that the calling program should pass a copy of the value of the argument in the method call to the parameter, which can be used in the method body. Section 7.13 discusses argument-passing options in detail.

A method can specify multiple parameters by separating each from the next with a comma. The number of arguments in a method call must match the number of parameters in the called method's parameter list. Also, the argument types in the method call must be "consistent" with the types of the corresponding parameters in the method's declaration. In our example, the method call (line 19 of Fig. 4.5) passes one argument of type String (nameOfCourse is declared as a String in line 13 of Fig. 4.5) and the method declaration specifies one parameter of type String (line 5 in Fig. 4.4), so the type of the argument in the method call is identical to the type of the parameter in the method header.

Updated UML Class Diagram for Class GradeBook

The UML class diagram in Fig. 4.6 models class GradeBook of Fig. 4.4. Like Fig. 4.1, this GradeBook class contains public operation DisplayMessage. However, this version of DisplayMessage has a parameter. The UML models a parameter a bit differently from Visual Basic, by listing the parameter name in the parentheses following the operation name, followed by a colon and the parameter type. The UML has several data types that are similar to the Visual Basic types. For example, UML types String and Integer correspond to Visual Basic types String and Integer, respectively. Unfortunately, the UML does not provide types that correspond to every Visual Basic type. For this reason, and to avoid confusion between UML types and Visual Basic types, we use only Visual Basic types in our UML diagrams. Class GradeBook's method DisplayMessage (Fig. 4.4) has a String parameter named courseName, so Fig. 4.6 lists courseName : String between the parentheses following DisplayMessage.

Figure 4.6. UML class diagram indicating that class GradeBook has a DisplayMessage operation with a courseName parameter of type String.

Категории