VB2008从入门到精通(PDF格式英文版)-第18部分
按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
3。 In general; how would you test whether or not a ponent that uses a database worked
properly? Outline the process with bulleted points。
…………………………………………………………Page 71……………………………………………………………
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 49
4。 In general; how would you test the correctness of writing data to a file? To help under
stand the nature of the problem; how do you know that an operating system manipulates
files properly?
5。 If the CLR did not provide for a mechanism to catch overflow and underflow conditions;
how would you ensure that overflow and underflow didn’t happen?
6。 For a Pentium CPU (32 bits); which number type would result in the fastest calculations?
7。 In this chapter’s example; the class Operations is designed to perform arithmetic using
the Double type。 How would you change this so that the calculations are generic and
don’t rely on the Double type?
…………………………………………………………Page 72……………………………………………………………
…………………………………………………………Page 73……………………………………………………………
C H A P T E R 3
■ ■ ■
Learning About String
Manipulations
In the previous chapter; you learned the basics of how data is stored and managed by ;
including the difference between value and reference types。 has three major data types:
number…related; custom…defined; and String。 The previous chapter focused on the number
related types。 This chapter will focus on the String type。
As you’ll learn in this chapter; the String type has some special characteristics。 If you were
to look at the bits and bytes of the String type; you would not realize that they were letters。 In
an abstract description; a String type is a number type with a special grammar。 Since a puter
understands only numbers; it uses lookup tables that map a set of letters to a set of numbers。
The example in this chapter is a multilingual translation program。 The translation program
will not be sophisticated; nor will it be capable of much。 However; it will illustrate many of the
issues that you will be confronted with when working with strings。
Organizing the Translation Application
As emphasized in the previous chapter; the first step in developing an application is to get
organized。 We need to understand and define the features of the sample application we are
going to develop。 The multilingual translation program will implement the following features:
o Translate greetings into three different languages: French; German; and English。
o Convert numbers into the three different languages。
o Convert a date into the three different languages。
From a feature perspective; the first feature is logical; but the second and third features are
not as obvious。 We generally think of translation as translating one word to another word(s)。
Yet; languages also can represent numbers and dates in different ways。 Translation will mean
two things: translate a word from one language to another; and translate a number or date
from one language to another。
As in Chapter 2; we’ll create the solution as ponents with three pieces: a Windows
application; a testing console application; and a class library。 After you have created each of the
projects; your workspace should look like Figure 3…1。 Remember to add a reference to the
51
…………………………………………………………Page 74……………………………………………………………
52 CH AP T E R 3 ■ L E A R N IN G AB OU T ST R I N G M A N I P U L AT IO N S
LanguageTranslator class library (right…click TestLanguageTranslator and choose Add Reference
Projects LanguageTranslator)。 Also remember to set TestLanguageTranslator as the
startup project。
Figure 3…1。 Structure of projects for the translation application in Visual Basic Express
Solution Explorer
Building the Translator Application
The translation application; like the calculator application example in the previous chapter; is
built in pieces: the class library that performs translations based on data delivered by the user
interface; the tests; and the user interface。 The individual pieces are ponents that can be fit
together like a jigsaw puzzle to create an application。
■Note ponents are a core part of your development toolbox。 As you will see throughout the book;
ponents allow you to reuse and modularize functionality。 ponents result in applications that are main
tainable and extendable。 Of course; there are limits; and the advantages are not automatic。 You will need to
properly design your application to benefit from using ponents。
Creating the Translator Class
When working with Visual Basic Express; or one of the other Visual Studio products; using the
default templates for creating a class library results in the creation of a file named Class1。vb。 It
is good that a default file is created for a class library; but the identifier Class1。vb does not imply
much。 Therefore; you should go ahead and delete that file from the project (simply renaming
the file does not rename the class within it)。 In its place; create the Translator class; as follows:
1。 Right…click the LanguageTranslator project。
2。 Click Add New Item。
3。 Select Class。
…………………………………………………………Page 75……………………………………………………………
CH AP T E R 3 ■ L E AR N IN G AB O U T ST R I N G M A N I PU L A TI O N S 53
4。 Rename the file Translator。vb。
5。 Click Add to create the file and add it to your project。
Notice how quickly you managed to create a Visual Basic class using the IDE。 The speed of
creating a class file lets you focus on adding source code to the file。 But do not be misled into
believing that by creating a number of class files; your code will automatically work and be a
masterpiece。 You still need to think about which files; projects; classes; and tests to create。
Translating Hello
The first feature we will implement is the translation of the text “hello。” Since “hello” is English;
the first translation will be English to German。 The following is the code to implement this
feature。 It is added to the Translator。vb file in the LanguageTranslator project。
Public Class Translator
Public Shared Function TranslateHello(ByVal input As String) As String
If (input。pareTo(〃hello〃) = 0) Then
Return 〃hallo〃
ElseIf (input。pareTo(〃allo〃) = 0) Then
Return 〃hallo〃
End If
Return
End Function
End Class
Translator is the main class that is exposed to other ponents or pieces of source code。
Think of it as the identifier of the black box。 The black box has a single method: TranslateHello()。
TranslateHello() is used to convert the French “allo” and the English “hello” to the German
“hallo。” The method’s input is a String type; which is a reference object type。
In the implementation of TranslateHello(); we pare the contents of the input buffer
to the parameter 〃hello〃。 If the parison is equal; meaning that the strings are equal; 0 is
returned。 As you’ll learn in the “Investigating the String Type” section; String is a class that can
be used to create objects; and objects typically have methods。 One of the String type’s methods
is pareTo()。 The caller of TranslateHello does not know how you managed to translate one
word to another language。 The caller actually does not care; it cares only that the method behaves
as expected。
The abstract intent of the TranslateHello() method is to accept some text and; if the text
is matched; return a German “hallo。”
Creating the Test Application
Without questioning the abstract intent; the written code needs some testing。 The test code is
added to the test application; which is the project TestLanguageTranslator。
The following code is added to the Module1。vb file。 Remember to import the
LanguageTranslator namespace; otherwise; the test code will not pile。
…………………………………………………………Page 76……………………………………………………………
54 CH AP T E R 3 ■ L E A R N IN G AB OU T ST R I N G M A N I P U L AT IO N S
Imports LanguageTranslator
Module Module1
Private Sub TestTranslateHello()
If (Translator。TranslateHello(〃hello〃)。pareTo(〃hallo〃) 0) Then
Console。WriteLine(〃hello to hallo test failed〃)
End If
If (Translator。TranslateHello(〃allo〃)。pareTo(〃hallo〃) 0) Then
Console。WriteLine(〃allo to hallo test failed〃)
End If
If (Translator。TranslateHello(〃allosss〃)。pareTo(〃〃) 0) Then
Console。WriteLine(〃Verify nontranslated word test failed〃)
End If
If (Translator。TranslateHello(〃 allo〃)。pareTo(〃hallo〃) 0) Then
Console。WriteLine(〃Extra whitespaces allo to hallo test failed〃)
End If
End Sub
Sub Main()
TestTranslateHello()
Console。ReadKey()
End Sub
End Module
The source code contains four tests。 Each calls the method TranslateHel