《VB2008从入门到精通(PDF格式英文版)》第22章


…………………………………………………………Page 56……………………………………………………………
34 CH AP T E R 2 ■ L E A R N IN G AB OU T 。 N E T N U M B E R A N D V A L U E T Y P E S
Function declaration implies that the caller expects to get a value back from the method。 The
data type of the returned value is specified by the As Integer keywords—this particular func
tion will return an Integer value。 (If you want to define a method that does not return a value;
use Sub rather than Function。) 
Methods and parameters must be associated with a type; as Visual Basic is a type…safe
programming language。 Type…safe means that when you write code; you know what you are
manipulating。 
Suppose that you are writing code and are confronted with the numbers 1; 1。0; and 〃1。0〃。
To you; these three numbers are identical。 But in the context of the source code; they are not
identical。 The 1 is an integer; the 1。0 is a double; and the 〃1。0〃 is a string。 When you want to
add; subtract; or otherwise manipulate pieces of data; they should be the same types; other
wise; you might run into consistency errors。 Type…safe programming languages help avoid
such problems。 The number types are discussed in more detail in the “Understanding the
CLR Numeric Types” section later in this chapter。 
The declaration of Add() says that we need to pass in two integer…based numeric values;
and the method returns an integer…based numeric value。 The bination of parameters and
a return type is a method signature。 The method signature bees important when another
piece of code calls the Add() method。 The other piece of code must use the same types as the
declaration。 Figure 2…7 shows a piece of code that calls the Add() method; which we’ll do from
another application in the next section。 
Namespace Class Method 
Dim total As Integer = Calculator。Operations。Add(1; 2) 
total is an integer declared 1 and 2 are integer values; 
variable that stores the result which represent the two 
of the addition numbers that are added together 
Figure 2…7。 The Add() method is called by referencing the namespace and class containing the
method。 A period is used to separate the identifiers。 
The caller must do two things:
o Reference the correct bination of namespace; class; and method identifiers。 
o Pass the correct types into the method; as specified by the method signature。 
In the example; the addition of 1 and 2 results in 3; and therefore the variable total should
contain the value 3 (the equal sign assigns the value returned from the method to the variable
on its left)。 I say “should contain the value;” because when writing code; you are not always
sure。 Sometimes the code you write will be wrong because you overlooked something or forgot
to reference something。
Look at the calling code; and ask yourself if you are guaranteed that calling Add() with 1
and 2 will result in 3。 The answer is that; as a caller; you cannot be 100% sure that the total variable
will contain 3。 Just because a box has the label “Dishes” does not necessarily mean that dishes
…………………………………………………………Page 57……………………………………………………………
CH A PT E R 2 ■ L E A R N I N G A B OU T 。 N E T N U M B E R AN D V A L U E T Y P E S 35 
are in the box。 You think you know the contents; but you cannot be 100% sure until you open
the box。 Likewise; in code; you need to look at how the Add() method is implemented to be sure
of the contents of the total variable。
In a production coding session; looking at the implementation code to verify it is doing
what you expect is not a feasible solution; because that would take too much time and be
pletely unreliable。 The only real solution is to write test code。
Writing Code to Test the Add() Method 
Test code is caller code that passes parameters with targeted values and expects a targeted
answer。 If the caller does not get the targeted answer; then the implementation of the tested
method is wrong。 Figure 2…8 shows sample caller code that tests the Add() operation (we’ll add
this to a project next)。
Targeted caller code that adds 
1 and 2 and assigns the result 
to the variable total 
Dim total As Integer = Operations。Add(1; 2) 
If (total 3) Then 
Console。WriteLine(〃Oops 1 add 2 does not equal 3〃) 
End If 
Targeted testing If targeted testing fails; the 
of the variable text “Oops…” is generated; 
total; paring it indicating an error 
to the value 3 
Figure 2…8。 Testing the Add() method 
The calling code of the test bears an uncanny resemblance to the code you saw in the previous
section。 The difference is that the test code uses targeted variables and values; whereas the
other code could contain any variables and values。 Another requirement of test code is to verify
the answers returned by the method with targeted responses。 The If sta
小说推荐
返回首页返回目录