如果条件为假,则跳过For循环的当前迭代

时间:2017-05-17 09:11:18

标签: vb.net for-loop if-statement iterator conditional-statements

我有一个大型子程序(1958行)来根据复杂的决策树计算一些价格和数字。

这些数字将位于多个表中的一个中,因此要检查是否有适用的数据集,我已经获得以下If statement

If dDt.Rows.Count = 0 And cDt.Rows.Count = 0 And p1Dt.Rows.Count = 0 And p2Dt.Rows.Count = 0 And p3Dt.Rows.Count = 0 Then
   If SysAQ = False Then
    Me.Cursor = Cursors.Default
    MessageBox.Show("There are no rates in the system for " & cmbSupplier.Text & "/" & ugr.Cells("Product_Code").Value & "/" & cmbCustCode.Text & " for todays " & _
           "date. Please add one and try again.", "No Rate Exists", MessageBoxButtons.OK, MessageBoxIcon.Information)

    ugr.Cells("DSC_Code").Value = ""
    ugr.Cells("DSC_Rate").Value = 0
    ugr.Cells("DSC_Value").Value = 0
    ugr.Cells("DSC_VAT").Value = 0

    Exit Sub
  ElseIf suppAQ = False Then
    Me.Cursor = Cursors.Default
    MessageBox.Show("There are no rates in the system for " & cmbSupplier.Text & "/" & ugr.Cells("Product_Code").Value & "/" & cmbCustCode.Text & " for todays " & _
               "date. Please add one and try again.", "No Rate Exists", MessageBoxButtons.OK, MessageBoxIcon.Information)

      ugr.Cells("DSC_Code").Value = ""
      ugr.Cells("DSC_Rate").Value = 0
      ugr.Cells("DSC_Value").Value = 0
      ugr.Cells("DSC_VAT").Value = 0

      Exit Sub
  ElseIf aDt.Rows.Count = 0 Then
      Me.Cursor = Cursors.Default
      MessageBox.Show("There are no rates in the system for " & cmbSupplier.Text & "/" & ugr.Cells("Product_Code").Value & "/" & cmbCustCode.Text & " for todays " & _
        "date. Please add one and try again.", "No Rate Exists", MessageBoxButtons.OK, MessageBoxIcon.Information)

       ugr.Cells("DSC_Code").Value = ""
       ugr.Cells("DSC_Rate").Value = 0
       ugr.Cells("DSC_Value").Value = 0
       ugr.Cells("DSC_VAT").Value = 0

       Exit Sub
    End If
End If

此代码块位于For LoopFor Each ugr As UltraGridRow in ugLines.Rows

之内

这意味着它会检查生产线上是否有价格,如果没有,则提醒用户并退出子程序。

此子例程在ugLines.AfterRowInsert,(添加产品后)以及保存完整数据之前调用。

它适用于后一种情况,因为每当它检测到没有价格的行时,它就会出错并且不能保存。但是,如果添加了没有价格的产品,那么用户会添加另一个产品,它将永远不会添加价格或告诉用户新线路缺少价格,因为第一个总是失败。

我想知道的是,是否还有另一种方法可以解决这个问题?我可以将其更改为跳过其余代码并直接跳转到下一次迭代,而不是Exit Sub。我已经考虑过使用ContinueContinue For,但这似乎只适用于检查相反的情况,以检查每个产品在至少一个表中的价格在哪里,制作If Statement更复杂 - 还有另一种解决方法吗?

1 个答案:

答案 0 :(得分:0)

基本上,只需将问题中的代码更改为以下内容:

Dim noRatesInSystem As Boolean = False
If dDt.Rows.Count = 0 And cDt.Rows.Count = 0 And p1Dt.Rows.Count = 0 And p2Dt.Rows.Count = 0 And p3Dt.Rows.Count = 0 Then
  If SysAQ = False OrElse suppAQ = False OrElse aDt.Rows.Count = 0 Then
     Me.Cursor = Cursors.Default
     MessageBox.Show("There are no rates in the system for " & cmbSupplier.Text & "/" & ugr.Cells("Product_Code").Value & "/" & cmbCustCode.Text & " for todays " & _
         "date. Please add one and try again.", "No Rate Exists", MessageBoxButtons.OK, MessageBoxIcon.Information)
          ugr.Cells("DSC_Code").Value = ""
          ugr.Cells("DSC_Rate").Value = 0
          ugr.Cells("DSC_Value").Value = 0
          ugr.Cells("DSC_VAT").Value = 0

          noRatesInSystem = True
   End If
End If

If noRatesInSystem = True Then Continue For

这将A)整理If Statement,B)如果找到价格继续循环并且C)如果没有找到价格,则跳过迭代(跳转到Next)。