13.07.2015 Views

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

72<strong>ASP</strong>.<strong>NET</strong> <strong>3.5</strong>: A Beginner’s <strong>Guide</strong>Figure 3-4 Output of simple for looploopcount variable (loopcount++) and the loop executes again. Figure 3-4 showsthe output you can expect to see.You can perform the same action with each iteration, or you can use the loop variableto generate different out<strong>com</strong>es.foreach LoopThe foreach loop is a favorite of mine because you can loop through an object with anunknown and/or changing collection of elements. For example, an array object containsany number of elements of specified types. (See the section on arrays later in this chapter.)Ask the ExpertQ: In the example the loop variable is initialized as a private variable outside of the loopstatement. Why is that?A: This is more a matter of style than either convention or necessity. I like to gather up all ofmy variables and put them in one place. Then when I set up the loop, I don’t have to enterthe type, reducing clutter. The only problem with that style is that when you have severaldifferent loops and you want to keep each local and reuse your favorite loop variable name,you may generate conflict.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!