How to use For Each loop in VB.NET

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
7 posts Page 1 of 1
Contributors
User avatar
Codex
Coding God
Coding God
Posts: 2028
Joined: Wed Mar 31, 2010 5:50 pm

How to use For Each loop in VB.NET
Codex
The For Each Loop is a iterative loop. Whenever you want to repeat a task in programming several time or a given condition in this situation we use loop statements to achieve your desired results. This article defines the For Each loop that how it repeat a task several time like For Next loop, While loop.

For Each loop

For loop is a iterative loop. it is used to execute a single item and item in group like every single element in an Array, or every single files in a folder or , every character in a String.

Example

If we want to display every character in the web site name "http://codenstuff.com" . In that situation for loop is very useful.
Code: Select all
Module Module1
    Sub Main()
        Dim Name As String
        Dim sinChar As Char
        Name = "http://codenstuff.com" 'Assigning the site name in a variable
        For Each sinChar In Name           'This line is extracting the single item from the group  
            Console.WriteLine(sinChar)
        Next
 
    End Sub
 
End Module

Now you execute this program you will get each character of the string in console window.


For Each loop with array

For Each loop is also used to work with array elements. the below example defines how to sort the array elements with for each loop.

For example

This example describes that how to sort the array elements.
Code: Select all
Module Module1
    Sub Main()
        Dim num As Integer() = New Integer(4) {}
        Console.WriteLine("Enter {0} number : ", num.Length)
        For i As Integer = 0 To num.Length - 1
            num(i) = Integer.Parse(Console.ReadLine())
        Next
        Array.Sort(num)
      Console.WriteLine("sorted array is :")
        For Each n As Integer In num
            Console.WriteLine(n)
        Next
    End Sub
 
End Module
Thank You for reading this tutorial, if it helps, comment, rate, +rep :D
CodexVideos
We shall let the revolution begin.. the revolution for freedom, freedom against censorship. We shall fight in the fields and in the streets, we shall fight in the hills; we shall never surrender
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

I remember this being posted on vbdotnetheaven.
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

mandai wrote:
I remember this being posted on vbdotnetheaven.
To be exact, it can be found here.
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Why don't people give credits to the person from whom he took the article/source? I hate it... Yesterday we were having a similar situation with CoderBoy.
Image
User avatar
Skillful
Skillful Coders
Skillful Coders
Posts: 969
Joined: Tue Nov 16, 2010 10:07 am

Usman55 wrote:
Why don't people give credits to the person from whom he took the article/source? I hate it... Yesterday we were having a similar situation with CoderBoy.
Usman55 if you check my tuts, I have given credits to VBDotNetHeaven at the bottom of the post.
Instead of LOL use this -
LSIBMHBIWFETALOL

Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

But you too at first said that you have been 'WRITING' tuts from the past 5 days and have posted 5 out of 8 till now. And I too when at first saw your tuts, there was no credits. I think you ave edited it lol. But you have did good by giving credits.
Image
User avatar
Skillful
Skillful Coders
Skillful Coders
Posts: 969
Joined: Tue Nov 16, 2010 10:07 am

Usman55 wrote:
But you too at first said that you have been 'WRITING' tuts from the past 5 days and have posted 5 out of 8 till now. And I too when at first saw your tuts, there was no credits. I think you ave edited it lol. But you have did good by giving credits.
Yes I had been writing tuts but then i lost interest in writing tuts, became tired of writing as i was not getting an idea of what to write, so i copied it.
Instead of LOL use this -
LSIBMHBIWFETALOL

Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
7 posts Page 1 of 1
Return to “Tutorials”