Results 1 to 3 of 3

Thread: [RESOLVED] For Each Loop test value if True before Next

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Resolved [RESOLVED] For Each Loop test value if True before Next

    What I want to achieve whit this code is to test first the value, If Return True, the next item will be process but if return False I will do something before calling the next item to be process

    Code:
    Private Sub loopRequestedCode()
            Dim isOk As Boolean
            Dim da As New MySqlDataAdapter()
            Dim ds As New DataSet()
            Dim dt As DataTable
            Dim dr As DataRow
            Dim conn As MySqlConnection = New MySqlConnection()
            Try
                conn.ConnectionString = clsComm.connString()
                Dim SQL As String = "SELECT * from tbl_request"
                conn.Open()
                da.SelectCommand = New MySqlCommand(SQL, conn)
                Dim myCB As New MySqlCommandBuilder(da)
                da.Fill(ds, "Request")
                dt = ds.Tables(0)
                For Each dr In dt.Rows
                    If isOk = sendRequest(dr(1).ToString(), dr(2).ToString()) Then
                        'goto next item
                    Else
                        'if false, do something before going to Next
                    End If
                Next
                ds.Dispose()
            Catch ex As Exception
                'TODO: catch error here
            End Try
        End Sub
    VB 6.0 = "Self-Study" Then
    vb.NET = "Self-Study" Then
    C# = 'on going study.....

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: For Each Loop test value if True before Next

    You're making this way more complicated than it needs to be. If you don't do anything inside the loop then it inherently goes on to the next item, so there's no point in an Else. You just need an If. Test a condition and do something if it's True, otherwise execution naturally moves on to the next iteration. If the condition you want to test is that 'sendRequest' returns False then that's what you should do:
    Code:
                For Each dr In dt.Rows
                    If sendRequest(dr(1).ToString(), dr(2).ToString()) = False Then
                        'if false, do something before going to Next
                    End If
                Next
    The preferred way of expressing that is:
    Code:
                For Each dr In dt.Rows
                    If Not sendRequest(dr(1).ToString(), dr(2).ToString()) Then
                        'if false, do something before going to Next
                    End If
                Next
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Re: For Each Loop test value if True before Next

    Hahaha...I almost forgot such kind of approach....maybe I'm sleepy...:@

    thanks jm.
    VB 6.0 = "Self-Study" Then
    vb.NET = "Self-Study" Then
    C# = 'on going study.....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width