|
-
Aug 13th, 2012, 10:39 PM
#1
Thread Starter
Hyperactive Member
[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.....
-
Aug 13th, 2012, 10:48 PM
#2
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
-
Aug 13th, 2012, 11:02 PM
#3
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|