Iterations in Visual Basic 2010 are performed via For..Next
and For Each
loops. Let’s analyze them more in details.
For..Next
A For..Next
loop enables repeating the same action (or group of actions) for a finite number of times. The following code shows an example in which the same action (writing to the Console window) is performed 10 times:
In such loops you need to define a variable of a numeric type (i
in the preceding example) that acts as a counter.
You may also assign the variable with another variable of the same type instead of assigning a numeric value.
The above code produces the following result:
Notice that you can also initialize the counter with zero or with any other numeric value.
Use Integer
or UInteger
variables as counters in For..Next
loops. This is because such data types are optimized for the Visual Basic compiler. Other numeric types are also supported but are not optimized, so you are encouraged to always use Integer
or UInteger
.
You can also decide how the counter must be incremented. For example, you could decide to increment the counter of two units instead of one (as in the previous example). This can be accomplished via the Step
keyword:
This code produces the following output:
Step
can also work with negative numbers and allows performing a going-back loop:
You can also decide to break a For
loop when a particular condition is satisfied, and you do not need to still perform the iteration. This can be accomplished with the Exit For
statement, as shown in the following example:
In the preceding example, when the counter reaches the value of 4, the For
loop is interrupted and control is returned to the code that immediately follows the Next
keyword. There is also another way for controlling a For
loop; there could be situations in which you need to pass the control directly to the next iteration of the loop when a particular condition is satisfied (which is the opposite of Exit For
). This can be accomplished with the Continue For
statement, as shown in the following code snippet:
In the preceding example we are doing some edits on the counter. Notice that each time you invoke a Continue For
, the counter itself is incremented one unit.
For Each
A For Each
loop allows performing an action or a group of actions on each item from an array or a collection. Although collections are discussed in Chapter 16, it is a good idea to provide a code example with them, because this is the typical usage of a For Each
loop. For example, consider the following code:
In the preceding code snippet, there is a collection containing references to all the running processes on the machine. Each process is represented by an instance of the System.Diagnostics.Process
class; therefore List(Of Process)
is a collection of processes. Supposing we want to retrieve some information for each process, such as the name and the identification number, we can iterate the collection using a For Each
statement. You need to specify a variable that is the same type of the item you are investigating. In the preceding code you are just performing reading operations, but you can also edit items’ properties. For example, you might have a collection of Person
objects, and you could retrieve and edit information for each Person
in the collection, as in the following code:
This code will add the Dr.
prefix to the LastName
property of each Person
instance.
For..Each
AvailabilityBehind the scenes, For Each
can be used against objects that implement the IEnumerable
or IEnumerable(Of T)
interfaces. Such objects expose the enumerator that provides support about For Each
iterations.
You can still use Exit For
when you need to break out from a For Each
statement. Basically a For Each
loop has better performances with collections than with arrays, but you have to know that you can use it in both scenarios.
18.217.146.61