Showing posts with label Loop. Show all posts
Showing posts with label Loop. Show all posts

Monday, April 18, 2011

Loop through a DataSet

        Dim dr As DataRow
        Dim dt As DataTable
        dt = DataSetName.Tables(0)
        For Each dr In dt.Rows
           something = dr("FeildName")
        Next

Wednesday, November 3, 2010

Looping through an enum

Basic class containing the enum:

Public Class WeightUnits

    Enum ItemWeightUnits
        gms = 1
        kgs = 2
        mls = 3
        ltr = 4
    End Enum

End Class

An instance of the WeightUnits class is not required as the enum is shared by default.

Loop through the enum and get the NAMES of each entry in the enum.

        Dim items As Array
        items = System.Enum.GetNames(GetType(WeightUnits.ItemWeightUnits))
        Dim item As String
        For Each item In items
            MsgBox(item)
        Next

Loop 1

Loop 2

Loop 3

Loop 4


Loop through the enum and get the VALUES of each entry in the enum.

        Dim items As Array
        items = System.Enum.GetValues(GetType(WeightUnits.ItemWeightUnits))
        Dim item As String
        For Each item In items
            MsgBox(item)
        Next

Loop 1

Loop 2

Loop 3

Loop 4

Use the Peek method of the StreamReader

HOW TO: Use the Peek method
Syntax
streamReaderVariableName.Peek

Example
Dim lineOfText As String
   Do Until inFile.Peek = -1
   lineOfText = inFile.ReadLine
   MessageBox.Show(lineOfText)
Loop
reads each line of the file associated with the inFile variable, line by line. Each line (excluding the newline character) is assigned to the lineOfText variable and is then displayed in a messagebox.

Wednesday, October 27, 2010

The For loop

The For loop is used when the number of repetitions is known. It uses a single variable to start the loop, and indicates how many times the loop needs to execute.

For intCount = 0 To 5
   msgbox("This is loop number " & intCount)

Next


The loop counter variable in this instance is intCount. It begins at 0. Each pass of the loop causes the loop counter to increment +1. The loop will execute 6 time - from 0 to 6.

What is a loop?

Loops allows a programmer to repeat a block of code a number of times while some condition is true, or until some condition becomes true. For instance, a loop could be used to prompt a user for an answer until the correct input is entered.

A loop must contain a starting condition and an ending condition. It is very important that a loop is able to reach an ending condition or we end up with what what is called an ‘infinite loop’ – a loop that never ends. This can cause our programs to behave abnormally and may even cause a computer to ‘crash’.