Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

C# - Break or Exit For Each Loop in VB.NET | Exit/Break For Loop in C#, VB.NET

Mar 26, 2013
Introduction

Here I will explain how break or exit for each loop in C#, VB.NET or Exit or break for loop in C#VB.NET

Description:
  
In previous articles I explained C#- Pass table as parameter to stored procedure, Delete selected row from datatable, C#- Split string with special characters and many articles relating to C#, VB.NET and
asp.net. Now I will explain how to exit or break for each loop in C#, VB.NET.

If you want to exit or break from for each loop we need use break statement for that we need write the code like as shown below

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

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

2 comments :

Anonymous said...

hello sir,,,
can u post any assignment on shopping cart

nagasudheers said...

Hi suresh,

Thank you for your postings it is very helpful to me . one advise is can you please post some
articles about LINQ it is very helpful to all the members who are following your website ... thank you........ sudheer.....

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.