The with statement and various loops available in Visual Basic.Net.


In this article I am going to discuss with statement and various loops like Do While, DO Loop, For Loop, while Loop etc available in Visual Basic.Net. These loops are actually very useful in Visual Basic .Net and all other programming languages.

In the previous article I show you how to use a Combobox and Listbox in Visual Basic.Net and populate them from the MYSQL Database table.
In this article I am going to discuss with statement and various loops available in Visual Basic.Net.
The with statement in Visual Basic.Net is as useful as a loop. We use with statement to execute many statements with a particular object.


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With TextBox1
.Text = "Sarfaraz"
.TextAlign = HorizontalAlignment.Center
.ForeColor = Color.Blue

End With
End Sub

DO Loop: - The do loop keeps the statements executing until the condition is true. To terminate the do loop we can use Exit do statement inside do loop. The do loop actually has two forms
Do while: - This loop keeps on executing the statements while or until a condition is ture.

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As Integer
Do While x < 10
ListBox1.Items.Add(x)
x = x + 1

Loop

End Sub


Do Loop: - This loop keeps on executing the statements until the condition is true. The only difference between Do Loop and Do while is that even if the condition is false Do Loop still executes the statement at least once.

Dim x As Integer
Do
ListBox1.Items.Add(x)
x = x + 1
Loop While x < 10


For Loop: - The For loop is the most popular loop in Visual Basic.Net and all other computer programming languages. In a for loop we know the initial value of the index and also the number of iterations it is going to execute.

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As Integer
For x = 1 To 50
ListBox1.Items.Add(x)
Next

End Sub

Actually the index is incremented by steps the default value is 1. We can change that also as shown below:

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As Integer
For x = 1 To 50 Step 2
ListBox1.Items.Add(x)
Next


End Sub


The For loop can also execute backwards like below

Dim x As Integer
For x = 50 To 1 Step - 2
ListBox1.Items.Add(x)
Next


For each loop: - This loop is actually used to loop over the elements of an array or a collection in visual basic.net.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each testfolder As String In My.Computer.FileSystem.GetDirectories("D:\")
ListBox1.Items.Add(testfolder)

Next

End Sub


While Loop: - This loop keeps the statements executing while the condition is true. Once the condition is false the loop terminates.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As Integer
While x < 10
ListBox1.Items.Add(x)
x = x + 1
End While
End Sub



Regards
Keep Coding


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: