Microsoft 9GD00001 Computer Accessories User Manual


 
148 Microsoft Visual Studio 2010: A Beginner’s Guide
This program calculates the total monetary discount that a customer would receive for that
order by calling the GetOrderDiscount method on the Customer instance, which then returns
the calculated discount amount to be subsequently displayed on the console. Essentially, we
created a couple of object instances, cust and ord, gave the object instances the data they
needed, and told the object instances to do some work for us. The result is a special discount
monetary amount for a given customer, based on the customer’s items ordered.
All of the code in the Main method is at the first level of the call hierarchy. The
methods and properties in Customer and Order are at the second level of the hierarchy.
Looking at Order, you can see that there is a Total property and an AddItem method.
AddItem adds the item parameter to its orderItems collection. Total iterates through the
orderItems collection, first calculating then returning the sum of all items. Notice that the
Customer class has a Discount property that holds a decimal value that will be used as a
percentage. The GetOrderDiscount method in Customer multiplies the Discount by the
Total in Order to return the discount of the order.
It’s important for you to study this example and understand the relationships and
communication between various objects. Observe that each class has a distinct purpose,
relating to how it is named. The purpose of the class helps decide what data and methods
that class will have; Order has Total and AddItem, and the class Customer has Discount
and GetOrderDiscount. Each object communicates with other objects, cooperating
to perform a task. For example, it is Customer’s responsibility to calculate a discount
because the Customer class knows what the discount should be (because we told it what
the discount was in Main). However, Customer must communicate with Order because
Order is the only object that knows about the order items and how to calculate the total.
Although I’ve shown you the code and explained how it works, it’s often useful to see
the flow of logic of the actual running program yourself. VS includes various visualization
and debugging tools that help you understand the flow of logic, which are discussed next.
Development-Time Code Tools
One of the new features of VS 2010 is Call Hierarchy, which allows you to see what
code calls a method and which methods are being called by your code. First, I’ll explain
why call hierarchy is important, and then I’ll show you how to use it. Figure 6-1 shows
what the Call Hierarchy window looks like, and the following discussion will explain the
motivation for and use of the Call Hierarchy feature.
The call hierarchy tells you several things about code, including the degree of reuse,
impact of a change, and potential importance of a routine. To help understand the discussion,
a call site is code that invokes another class member. For example, in Listing 6-1, the Main
method is the call site and the GetOrderDiscount method is the called code.