2012-02-24 153 views
1

嘿傢伙我是一個老屁,這是我的第一篇文章,所以請善待。我正在使用基於測量機器上的基礎的專有語言。爲了測試目的,我故意將前兩項檢查設置爲失敗。尺寸X的第一組ifs很好。當代碼運行並且它到達Size_Y時,如果操作符選擇「不,我不想重新測量」(返回7),則代碼跳轉到最後一端。我懷疑我不適當地嵌套我的Ifthens,但我看不到它。嵌套if語句跳過其他ifs

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

歡迎使用stackoverflow!我格式化了一下。它實際上看起來很像我的Visual Basic ... – MPelletier 2012-02-24 15:11:35

回答

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的語句是自己如果塊

來自:

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 
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每次都會變得真實。我假設這是爲了測試目的。