Pages

Wednesday, 3 July 2013

How To Break or Exit For Each Loop in VB.NET Or Exit/Break For Loop in C#, VB.NET Asp.Net

How To Break or Exit For Each Loop in VB.NET Or Exit/Break For Loop in C#, VB.NET Asp.Net

C# Code: for loop example

for (int i = 0; i < 10; i++)
{
int j = 4;
if (j == i)
{
break;
}
}

for each loop example

string listingId = string.Empty;
foreach (GridViewRow gvRow in grdSearchResults.Rows)
{
if (gvRow.Cells[2].Text != "0")
{
listingId = gvRow.Cells[2].Text;
}
if (!string.IsNullOrEmpty(listingId))
{
break;
}
}

VB Code: for loop example

For i As Integer = 0 To 9
Dim j As Integer = 4
If j = i Then
Exit For
End If
Next

for each loop example

Dim listingId As String = String.Empty
For Each gvRow As GridViewRow In grdSearchResults.Rows
If gvRow.Cells(2).Text <> "0" Then
listingId = gvRow.Cells(2).Text
End If
If Not String.IsNullOrEmpty(listingId) Then
Exit For
End If
Next

No comments:

Post a Comment