Summary


Control statements form the heart of any program. Decision statements determine what commands are executed, and looping statements determine how many times they are executed.

Single-line and multiline If Then statements, as well as Select Case, are the most commonly used decision statements. IIf and Choose statements are often more confusing and sometimes slower, so usually you should use If Then and Select Case statements instead. Under some specific circumstances, however, IIf and Choose may make your code more readable. Use your judgment and pick the method that makes the most sense in your application.

For Next, For Each, and Do Loop are the most common looping statements. Some container classes also support enumerators that let you step through the items in the container. An enumerator can be more natural than a For Each loop under some circumstances.

While End is equivalent to Do While Loop. You can use whichever you think makes more sense, although you might want to use Do While Loop because it is more consistent with the other forms of Do Loop.

Finally, the GoTo statement is often used in a decision statement or to create a loop. Unfortunately, undisciplined use of GoTo statements can lead to spaghetti code that is extremely hard to understand, debug, and maintain. To avoid later frustration, you should comment GoTo statements whenever possible. Some programmers use GoTo in very specialized cases, while others avoid it at all costs. You can always rewrite code to avoid GoTo statements, and usually that is better in the long run.

Using the control statements described in this chapter, you can build extremely complex and powerful applications. In fact, you can build applications that are so complex that it is difficult to ensure that they work correctly. Even a relatively simple application sometimes encounters errors. For example, it might try to use an array index that lies outside of the array’s bounds or to modify a collection while using an enumerator for it. Chapter 8 explains how you can protect an application from these and other unexpected errors and let it take action to correct any problems or at least not crash.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

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