VB.Net-Informations.com
   Home      .Net Framework      VB.NET      C#                                                                      About


  How to use FOR EACH loop in VB.NET

Whenever you face a situation in programming to repeat a task for several times (more than one times ) or you have to repeat a task till you reach a condition, in these situations you can use loop statements to achieve your desired results.

FOR NEXT Loop, FOR EACH Loop , WHILE Loop and DO WHILE Loop are the Commonly used loops in Visual Basic.NET 2005 ( VB.NET 2005) .

For Each Loop

FOR EACH Loop usually using when you are in a situation to execute every single element or item in a group (like every single element in an Array, or every single files in a folder or , every character in a String ) , in these type of situation you can use For Each loop.

  
For Each [Item] In [Group]
   [loopBody]

Next [Item]

Item : The Item in the group

Group : The group containing items

LoopBody : The code you want to execute within For Each Loop

Let's take a real time example , if you want to display the each character in the website name "HTTP://NET-INFORMATIONS.COM" , it is convenient to use For Each Loop.

  
1. siteName = "HTTP://NET-INFORMATIONS.COM"
2. For Each singleChar In siteName
3.	MsgBox(singleChar)

4. Next

Line 1: Assigning the site name in a variable

Line 2: This line is extracting the single item from the group

Line 3: Loop body

Line 4: Taking the next step

         VB.NET Source Code Download           Print Source Code
         How to use FOR EACH loop in VB.NET - Download
        
C# Tutorial

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, 
	ByVal e As System.EventArgs) Handles Button1.Click
        Dim siteName As String
        Dim singleChar As Char
        siteName = "HTTP://NET-INFORMATIONS.COM"
        For Each singleChar In siteName
            MsgBox(singleChar)
        Next
    End Sub
End Class

When you execute this program you will get each character of the string in Messagebox.


VB.NET Program Flow Control Related Contents
*     How to use IF ELSE END IF in VB.NET
*     How to use FOR NEXT loop in vb.net
*     How to use vb.net While End While loop


   Home      VB.NET      C#
VB.Net Related Topics
*     Microsoft .Net Framework Tutorials
*     VB.NET Language Basics Tutorials
*     VB.NET Program Flow Control Tutorials
*     VB.NET Collections Tutorials
*     VB.NET String Tutorials
*     VB.NET Files Tutorials
*     VB.NET Excel 2007 Tutorials
*     VB.NET Crystal Reports Tutorials
*     VB.NET Communications Tutorial
*     VB.NET ADO.NET Tutorial
*     ADO.NET Data Providers help and Tutorial
*     VB.NET ADO.NET Dataset Tutorial
*     ADO.NET DataAdapter and Dataset
*     VB.NET ADO.NET DataView Tutorial
*     VB.NET Remoting Tutorial
*     VB.NET XML Tutorial
*     VB.NET DataGridView Tutorial
   Home      VB.NET      C#
More Source Code :   
Mail to :  feedback@net-informations.com
  |  Home   |  VB.NET   |  C#   |  SiteMap   |  Terms of Use   |  About   |
net-informations.com (C) 2010
All Rights Reserved. All other trademarks are property of their respective owners.