嵌套if语句跳过其他ifs

时间:2012-02-24 14:58:54

标签: vb.net

嘿伙计们我是一个老屁,这是我的第一篇文章,所以请善待。我正在使用基于测量机基础的专有语言。我将delcritly设置前两个检查失败以进行测试。尺寸X的第一组ifs效果很好。当代码运行并且它达到Size_Y时,如果操作员选择“不,我不想重新测量”(返回7),则代码会一直跳到最后一端。我怀疑我没有正确地嵌套我的Ifthens,但我看不到它。

Private Sub CheckSpec

'give operator a message if the measure is out of spec

StartAgain:

If Size_X <= 3.125 OR Size_X >= 3.125 then 'actual spec
    'Warn that measure is not in spec and ask for remeasure

    BoxPick=Msgbox( "Measurement in Zone " & Zone & " for Die Size in X is not in spec.  Do you want to measure it again?" , 4 , "Measurment NOT Within Tolerance" ) 
    If BoxPick = 6 THEN 'if operator wants to remeasure, measure again and start checks over
        Call Measure_Die
        GoTo StartAgain
    ElseIF BoxPick <> 7 Then 'If value returned is NOT 6 or 7, throw error
        OperatorMsg "An error has occured.  Contact the tool owner"
        Call Unload
    ElseIF BoxPick = 7 Then 'If operator chooses not to remeasure then keep going
    Else
    End If


Elseif Size_Y <= 1.925 OR Size_Y >= 1.925 then
    'Warn that measure is not in spec and ask for remeasure

    BoxPick=Msgbox( "Measurement in Zone " & Zone & " for Die Size in X is not in spec.  Do you want to measure it again?" , 4 , "Measurment NOT Within Tolerance" ) 
    If BoxPick = 6 THEN 'if operator wants to remeasure, measure again and start checks over
        Call Measure_Die
        GoTo StartAgain
    ElseIF BoxPick <> 7 Then 'If value returned is NOT 6 or 7, throw error
        OperatorMsg "An error has occured.  Contact the tool owner"
        Call Unload
    ElseIF BoxPick = 7 Then 'If operator chooses not to remeasure keep going
    Else
    End If


Elseif Centration_X <= 0.175 OR Centration_X >= 0.225 then

    'Warn that measure is not in spec and ask for remeasure
    BoxPick=Msgbox( "Measurement in Zone " & Zone & " for Die Size in X is not in spec.  Do you want to measure it again?" , 4 , "Measurment NOT Within Tolerance" ) 
    If BoxPick = 6 THEN 'if operator wants to remeasure, measure again and start checks over
        Call Measure_Die
        GoTo StartAgain
    ElseIF BoxPick <> 7 Then 'If value returned is NOT 6 or 7, throw error
        OperatorMsg "An error has occured.  Contact the tool owner"
        Call Unload
    ElseIF BoxPick = 7 Then 'If operator chooses not to remeasure keep going
    Else
    End If


Elseif Centration_Y <= 0.95 OR Centration_Y >= 1.0 then

    'Warn that measure is not in spec and ask for remeasure
    BoxPick=Msgbox( "Measurement in Zone " & Zone & " for Die Size in X is not in spec.  Do you want to measure it again?" , 4 , "Measurment NOT Within Tolerance" ) 
    If BoxPick = 6 THEN 'if operator wants to remeasure, measure again and start checks over
        Call Measure_Die
        GoTo StartAgain
    ElseIF BoxPick <> 7 Then 'If value returned is NOT 6 or 7, throw error
        OperatorMsg "An error has occured.  Contact the tool owner"
        Call Unload
    ElseIF BoxPick = 7 Then 'If operator chooses not to remeasure keep going
    Else
    End If
Else

End If
Print #1, Column & "," & Row & "," & Level & "," &  Zone & "," & Size_X & "," & Size_Y & "," & Centration_X & "," & Centration_Y & "," & RightNow

End Sub 'CheckSpec

2 个答案:

答案 0 :(得分:1)

当你有一个像

这样的表达式时
If (2 + 2 == 4) then
     do something
ElseIf (3 + 3 == 6) then
     code never gets here, even though it is true
End If

可能最快的变化是将主要的ElseIf语句更改为自己的If块

来自:

Elseif Size_Y <= 1.925 OR Size_Y >= 1.925 then

为:

End If 'Size_X block check end
If Size_Y <= 1.925 OR Size_Y >= 1.925 then

然后从:

Elseif Centration_Y <= 0.95 OR Centration_Y >= 1.0 then

为:

End If 'Size Y block end
If Centration_Y <= 0.95 OR Centration_Y >= 1.0 then

答案 1 :(得分:0)

如果您已经位于IF块的“真实”部分之一,则无法输入ELSEIF块的其他部分。你已经在街区内了,唯一剩下的就是最后一个END-IF。

此外,在大多数情况下,GOTO并不是最好的做事方式。

考虑这样的事情,根据需要重构:

Public Enum MeasureResponse
  StartOver
  KeepGoing
  UnloadApp
End Enum

Private Function CheckTolerance(ByVal measureValue As Decimal, ByVal lowTolerance As Decimal, ByVal highTolerance As Decimal) As MeasureResponse
  Dim result As MeasureResponse = MeasureResponse.KeepGoing

  If measureValue <= lowTolerance Or measureValue >= highTolerance Then
    Select Case MessageBox.Show("Measurement in Zone Z for Die Size in X is not in spec.  Do you want to measure it again?", "Measurement NOT Within Tolerance", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
      Case Windows.Forms.DialogResult.Yes
        Call Measure_Die()
        result = MeasureResponse.StartOver
      Case Windows.Forms.DialogResult.Cancel
        result = MeasureResponse.UnloadApp
    End Select
  End If

  Return result
End Function

然后你的CheckSpec函数看起来像这样:

Private Sub CheckSpec()
  For i As Integer = 1 To 4
    Dim checkItem As Decimal = Choose(i, size_X, size_Y, centration_X, centration_Y)
    Dim lowTolerance As Decimal = Choose(i, 3.125, 1.925, 0.175, 0.95)
    Dim highTolerance As Decimal = Choose(i, 3.125, 1.925, 0.225, 1.0)

    Select Case CheckTolerance(checkItem, lowTolerance, highTolerance)
      Case MeasureResponse.StartOver
        i = 0
      Case MeasureResponse.UnloadApp
        Exit Sub
    End Select
  Next

  Print #1, Column & "," & Row & "," & Level & "," &  Zone & "," & Size_X & "," & Size_Y & "," & Centration_X & "," & Centration_Y & "," & RightNow
End Sub

这是在VB.Net中,但它应该可以翻译成VB6或者你正在使用的任何基本语言。

您使用的示例公差也没有意义。 Size_X <= 3.125 OR Size_X >= 3.125每次都会成真。我认为那是出于测试目的。