ASP.NET Razor VB loops, and arrays

Statements in the cycle will be repeated.


For loop

If you need to repeat the same statement, you can set up a cycle.

If you want to know the number of cycles, you can use the for loop. This type of loop is especially useful when counting up or counting down:

Examples

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

Running instance »


For Each loop

If you are using a collection or array, you will be frequently used for each cycle.

A collection is a group of similar objects, for each loop can walk through the collection until completion.

The following examples, traversing ASP.NET Request.ServerVariables collection.

Examples

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

Running instance »


While loop

while loop is a common cycle.

while loop begins with the keyword while, followed by a parenthesis, you can specify how long the cycle will then repeat the code block is executed in parentheses.

while loop is usually set a variable to increment or decrement the count.

The following example, the + = operator to perform a loop at each value of the variable i is incremented.

Examples

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

</body>
</html>

Running instance »


Array

When you want to store a plurality of similar variables you do not want to have to create a separate variable for each variable but you can use an array to store:

Examples

@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>

Running instance »