按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 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