Examining the Source Code


While we've yet to take an in-depth look at the Visual Basic language, I believe it will still prove worthwhile to take a moment to go over the source code for the ASP.NET page we just created. We've already discussed the "filler" code inserted automatically by Visual Web Developerthe class definition and Inherits statement, as well as the event handlerso let's concentrate on the code within the event handler. This is the code that is executed when the user clicks on the Compute Monthly Cost button. Its aim is to read in the user's inputs and calculate and display what the monthly mortgage costs.

The first line of code in the event handler (line 5) is a comment. In Visual Basic, comments are denoted by an apostrophe ('); all other content after an apostrophe is ignored by the ASP.NET engine. Comments serve a simple purpose: to make source code more readable by humans. Throughout the code examples in this book, you'll find helpful comments littered throughout, making it easier for you to read and understand what's happening.

After the first comment are two constants. A constant is a value that cannot be changed programmatically. The two constants hereINTEREST_CALCS_PER_YEAR and PAYMENTS_PER_YEARdefine how often the interest is compounded and how many pay periods will occur throughout the life of the loan. By default these are both 12, indicating that the loan compounds every month and payments are made once per month. Next, on lines 10 through 12, the values from the three TextBox Web controls are read into variables. A variable is a construct that holds some value. The variables defined here are named P, r, and t, and are of type Double.

The type of a variable indicates what sort of information it can hold. A Double is a predefined type in the .NET Framework that can hold a vast array of various numbers of varying precision.

After these TextBox Web control values have been read in, we apply the mathematical formulas needed to compute the monthly payment due.

On lines 14 and 15 the variable ratePerPeriod is created and assigned the value of the interest rate each time it's compounded. Following that, the number of pay periods is recorded and stored in the payPeriods variable. Next, the annual interest rate is determined as well as how much interest per payment is due (and then how much interest is due per month). Finally, the cost per month is computed on line 30. The resulting output is then assigned to the Text property of the results Label Web control.

The source code in Listing 4.1 is fairly readable, although the mathematical formulas might seem foreign. But I hope you'll agree that if you simply read through the lines of code, one at a time, it is clear what is happening. For example, on line 18, payPeriods = t * PAYMENTS_PER_YEAR, we are taking the value of t (the loan's term) and multiplying it by the number of payments made per year. This gives us the total number of payments made throughout the life of the loan. The point is, with Visual Basic's English-like syntax and semantics, even to someone with little to no programming background, it's possible to pick up the gist of the code by simply reading through it.

Don't worry if you still feel lost and bewildered by Listing 4.1. Your code-reading acumen will greatly improve over the next three hours as we delve into the syntax of Visual Basic. By the time you finish working through Hour 7, "Working with Objects in Visual Basic," you'll have the Visual Basic background necessary for understanding all of the examples throughout this book.




Sams Teach Yourself ASP. NET 2.0 in 24 Hours, Complete Starter Kit
Sams Teach Yourself ASP.NET 2.0 in 24 Hours, Complete Starter Kit
ISBN: 0672327384
EAN: 2147483647
Year: 2004
Pages: 233

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net