梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
that best suits your needs with the least number of critical promises。
´´´´´´´´´´´´´´´´´´´´´´Page 134´´´´´´´´´´´´´´´´´´´´´´´
112 CH AP T E R 4 * L E A R N IN G AB OU T D AT A S TR U CT U R E S�察 �DE CI SI ON S�察 �A N D L O OP S
o Data structures and algorithms do not need to be the same class。 They can be different
types and often are。
o Data structures can be implemented using value ��Structure�� or reference ��Class�� types。
o Value types when used as data structures have three constraints that you need to be
aware of that relate to the fact that data is copied�察�what happens when you embed refer
ence types in value types�察�and what happens when you use value types as parameters to
methods。
o For the most part�察�you will use reference types�察�but you can also use value types。 When
using value types�察�you need to be aware of how a value behaves�察�otherwise�察�you might get
undesirable interactions。
o A constructor is a special type of method that is called when a type is being instantiated。
You would assign parameters to a constructor when you want to enforce a verifiably
correct state for the object。
o A rule of thumb when trying to decide whether to use value and reference types is to
consider the context。 Are you creating a simple assign´once structure�察�or are you
creating a plex navigable structure�拭�If your structure is plex�察�then use a refer
ence type�察�otherwise�察�a value type is fine。
o When you instantiate a type�察�each object has its own set of instances of methods and
data members。 When a type has methods or data members declared with the Shared
keyword�察�that type has a single instance of the shared method or data member and is not
associated with a type instance。
o Writing the test before the type implementation allows a developer to get a feeling of
how the type should look and behave�察�and gives some guidance。
o When you write methods�察�you don¨t want to rely too heavily on magic data making
everything work。 When writing classes�察�you need to think in terms of IKEA furniture
��assembly required���察�as that will make your code more flexible and a candidate for reuse。
o When you write a For loop�察�think of the statements in the brackets as being code that
generates an index that is used to retrieve the actual information being iterated over。
o Decisions are implemented using a bination of If�察�ElseIf�察�and Else statements。
Some Things for You to Do
The following are some exercises to practice what you learned in this chapter。
1。 Node was declared to be a reference type。 Can you think of where in the declaration
of Node it would be more appropriate to use a value type�拭�And if you can think of it�察 �
rewrite Node。
2。 The shared data member Node。RootNodes is exposed for every class to consume。 Is there
a way to decouple RootNodes so that the user of Node is not aware of the location of
the tree�拭�
´´´´´´´´´´´´´´´´´´´´´´Page 135´´´´´´´´´´´´´´´´´´´´´´´
CH AP T E R 4 * L E A R N I N G A B OU T D AT A S TR U CT U R E S�察 �DE CI SI ON S�察 �A N D L O OP S 113
3。 We discussed a keyhole problem regarding the allocation of an array。 Yet there is also a
coupling problem between Node and DepthFirstSearch。 Explain why there is a coupling
problem and outline an alternative algorithm that does not have the coupling problem。
4。 Fix the CanContinueSearch�┌� function so that an optimal flight path is found for any two
cities。 Note that you should extend your test cases to test various scenarios。
5。 Implement the breadth´first search algorithm。 The breadth´first algorithm will search
each connection before going down further in the tree。 Hint�此�modify the behavior of
FindNextLeg�┌�。
´´´´´´´´´´´´´´´´´´´´´´Page 136´´´´´´´´´´´´´´´´´´´´´´´
´´´´´´´´´´´´´´´´´´´´´´Page 137´´´´´´´´´´´´´´´´´´´´´´´
C H A P T E R 5
* * *
Learning About Visual Basic
Exception Handling
Source code can have thousands�察�hundreds of thousands�察�or millions of lines of source code�察 �
and no single human could keep track of it all。 To keep track of all the source code�察�you need a
team of developers�察�and that means code written by one developer is going to be used and
modified by another developer。 Since the two developers can¨t perform a Vulcan mind meld�察 �
they must have a well´understood and useful form of munication。 But that¨s just part of
the solution。 The code itself must be easy to understand。
The challenge of writing software is not creating a perfect piece of code�察�but writing code
that can be understood by other developers and used by other pieces of software。 The goal isn¨t
to be clever and write software that can do everything�察�but to write simple�察�robust�察�and easy´to
understand software。 The ^keep it simple ̄ approach is the best way forward。
Having understandable code is particularly important when things go wrong。 Your code
should generate the appropriate errors。 For example�察�suppose your code relies on a file being
present。 When the file is not present�察�your code should generate a clear and distinct error�察�such
as ^File XYZ is not present and thus I cannot continue。 ̄ Upon seeing such an error message�察 �
another developer would know that he should check whether the file is actually there。
This chapter explains exceptions�察�as errors in an application are technically known�察�and how
to handle them。 We¨ll begin with an overview of how exceptions fit into the structure of a program。
Understanding Errors�察�Exceptions�察�and
Exception Handling
An error is when something is not right and could be the result of incorrect data or an incorrect
calculation。 However�察�the CLR does not understand errors�察�it understands only exceptions。
For example�察�if an error is caused by multiplying two numbers rather than adding them�察 �
your program will continue to function�察�but will produce the wrong results。 A similar error occurs
when a user enters the wrong data!the answer will be wrong�察�but the program will still run。
In the case of a serious problem that is beyond the control of the user or that threatens to crash
your program�察�the CLR steps in and treats this as an exception。 This interrupts the program and
allows your code to deal with the problem�察�rather than letting the program crash。 ��Some would
argue that an exception will not halt your entire program�察�but a thread of your program�察�while
115
´´´´´´´´´´´´´´´´´´´´´´Page 138´´´´´´´´´´´´´´´´´´´´´´´
116 CH AP T E R 5 * L E A R N IN G AB OU T V I SU A L B AS IC E X C E P TI ON H AN D L IN G
this is technically true�察�it¨s not an important distinction for this introduction to exceptions。��
This is called exception handling。
To understand how code organization affects exception handling�察�think of an application
as a large corporation。 A corporation has a chief executive officer ��CEO���察�then first´level managers�察 �
then mid´level managers。 Corporations and management within corporations understand
that to get an