ASP.NET Razor - VB Loops and Arrays

Statements can be executed repeatedly in loops.


For Loops

If you need to run the same statements repeatedly, you can program a loop.

If you know how many times you want to loop, you can use a for loop. This kind of loop is especially useful for counting up or counting down:

Example

<html>
<body>
@For i=10 To 21
    @<p>Line #@i</p>
Next i
</body>
</html>


For Each Loops

If you work with a collection or an array, you often use a for each loop.

A collection is a group of similar objects, and the for each loop lets you carry out a task on each item. The for each loop walks through a collection until it is finished.

The example below walks through the ASP.NET Request.ServerVariables collection.

Example

<html>
<body>
<ul>
@For Each x In Request.ServerVariables
    @<li>@x</li>
Next x
</ul>
</body>
</html>



While Loops

The while loop is a general purpose loop.

A while loop begins with the while keyword, followed by parentheses, where you specify how long the loop continues, then a block to repeat.

While loops typically add to, or subtract from, a variable used for counting.

In the example below, the += operator adds 1 to the variable i, each time the loop runs.

Example

<html>
<body>
@Code
Dim i=0
Do While i<5
    i += 1
    @<p>Line #@i</p>
Loop
End Code

</body>
</html>


Arrays

An array is useful when you want to store similar variables but don't want to create a separate variable for each of them:

Example

@Code
Dim members As String()={"Jani","Hege","Kai","Jim"}
i=Array.IndexOf(members,"Kai")+1
len=members.Length
x=members(2-1)
end Code
<html>
<body>
<h3>Members</h3>
@For Each person In members
   @<p>@person</p>
Next person

<p>The number of names in Members are 
@len</p>
<p>The person at position 2 is 
@x</p>
<p>Kai is now in position 
@i</p>
</body>
</html>

 

Razor VB Loops

Login
ADS CODE