嗔秤戻幣�哉膵�云利匈嬉蝕湊蛸賜�塋床四衲���萩晦編報炎嘔囚^泡仟 ̄云利匈��
VB2008貫秘壇欺娼宥(PDF鯉塀哂猟井)-及12何蛍
酔楯荷恬: 梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈 梓囚徒貧議 Enter 囚辛指欺云慕朕村匈 梓囚徒貧圭�鮗� ● 辛指欺云匈競何! 泌惚云慕短嗤堋響頼���誅卒亮茂�俊彭堋響��辛聞喘貧圭 "辺茄欺厘議箝誓匂" 孔嬬 才 "紗秘慕禰" 孔嬬��
We could use one of three types to declare the Add�┌� method�此�
o Long�此�Solves the problem of adding two very large numbers like 2 billion�察�but has the
problem that you cannot add fractional numbers like 1。5 plus 1。5。
o Double�此�Solves the problem of adding two very large or small numbers�察�and can be used
to add fractional numbers。 Generally speaking�察�Double is a good choice�察�but can suffer
from significance problems if a very large number is manipulated by a very small number。
o Decimal�此�A generally good approach and suitable for all types of precision�察�but also the
slowest when adding�察�subtracting�察�or performing other mathematical operations。
The simplest all´around numeric data type to use is Double�察�as it provides good precision
and is relatively fast。 The plete implementation of the calculator is as follows�此�
Public Class Operations
Public Shared Function Add��ByVal number1 As Double�察�ByVal number2 As _
Double�� As Double
Return number1 �� number2
End Function
Public Shared Function Divide��ByVal number1 As Double�察�ByVal number2 As _
Double�� As Double
Return number1 / number2
End Function
Public Shared Function Multiply��ByVal number1 As Double�察�ByVal number2 As _
Double�� As Double
Return number1 * number2
End Function
Public Shared Function Subtract��ByVal number1 As Double�察�ByVal number2 As _
Double�� As Double
Return number1 number2
End Function
End Class
The four operations are methods with different identifiers�察�but identical method signa
tures�察�making it easy to remember how to use each method。 Each of the operations would have
an appropriate set of tests verifying the correctness of the implementation。 The tests are not
reproduced here�察�but they are implemented in the sample source code。 I advise you to take a
quick look at the tests to make sure you understand the individual pieces。
´´´´´´´´´´´´´´´´´´´´´´Page 70´´´´´´´´´´´´´´´´´´´´´´´
48 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
The Important Stuff to Remember
In this chapter�察�you learned about developing a class library that is used to perform some
calculations。 The following are the key points to remember�此�
o Organization of your thoughts�察�projects�察�and features makes all the difference when
writing software。
o When writing software�察�stay focused。 It is very easy to drift around in software development�察 �
because software lets you stray easily。 A successful developer will always be organized
and focused。
o Software is designed using an architecture that could be implemented top´down or
bottom´up。
o Within an architecture�察�individual pieces are called ponents�察�and they fit together to
create a plete application。
o You write tests because you cannot verify the functionality of a ponent based on its
identifier�察�parameters�察�or return value。
o When implementing ponents�察�you develop tests before�察�during�察�and after writing
the source code。
o A test is a piece of source code that calls a ponent using targeted input data�察�and the
results from the ponent are verified with targeted responses。 If the results do not
match the targeted responses�察�the ponent has failed。
o The CLR offers many different data types�察�with the major distinction being between
value and reference types。
o The CLR has many different number types�察�but all number types are value types。
o Numbers can overflow or underflow。 You should activate a piler setting to make
sure that the CLR will catch those situations。
o When deciding on a specific number type�察�a large part of the decision is based on how
much precision is desired。
Some Things for You to Do
The following are some things to consider related to what you¨ve learned in this chapter�此�
1。 When you write code�察�how should you organize your code�拭�For example�察�do you enforce
certain naming conventions for your classes�拭�Do you enforce the use of code ments�拭�
2。 In the development munity�察�there is a discussion of whether organization of your
software should involve formal structures or should be ad hoc。 Think about how software
should be organized。
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 TranslateHello�┌� with some
酔楯荷恬: 梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈 梓囚徒貧議 Enter 囚辛指欺云慕朕村匈 梓囚徒貧圭�鮗� ● 辛指欺云匈競何!
梁椣戻幣�� 梁心弌傍議揖扮窟燕得胎��傍竃徭失議心隈才凪万弌誌育断蛍�輌臆惨軼僑〃�燕慕得珊辛參資誼持蛍才将刮襲潜��範寔亟圻幹慕得 瓜寡追葎娼得辛參資誼寄楚署衛、持蛍才将刮襲潜填��