2012-02-21 2 views
1

다른 결과가있는 중첩 된 else if 문을 처리하는 더 좋은 방법이 있습니까? 여기 중첩 된 다른 방법 If 다른 결과가있는 If 문?

설명 내 중첩 된 문 중 하나의 예입니다

  If My.Computer.Network.Ping(computerName) = True Then 
       Call InstallVS(computerName) 
       If My.Computer.Network.Ping(computerName) = True Then 
        Call PEC(computerName) 
        If My.Computer.Network.Ping(computerName) = True Then 
         Call RemoveSoftware(computerName) 
        Else 
         Call WriteLog(computerName & " lost connectivity while attemping to remove the temp software") 
        End If 
       Else 
        Call WriteLog(computerName & " lost connectivity while Forcing Communication") 
       End If 
      Else 
       Call WriteLog(computerName & " lost connectivity while attemping to Install") 
      End If 

내가 문 이러한 유형의 많은에 대한 요구 사항이, 일부는 일부는 훨씬 더 크다, 작다.

+0

참고. 그것은 당신이 보여주고 자하는 행동을 보여줍니다. 아래의 대답은 올바른 결과를 제공합니다. 왜냐하면 예외를 throw하는 경우에는 AFTERWARDS – Martin

답변

3

당신은 연결을 테스트하거나 특정 오류 메시지와 함께, 그렇지 않으면 예외가 발생합니다 PingOrFail라는 방법을 만들 수 있습니다.

Try 
    PingOrFail(computerName, "attempting to install") 
    Call InstallVS(computerName) 

    PingOrFail(computerName, "forcing communications") 
    Call PEC(computerName) 

    PingOrFail(computerName, "removing temp software") 
    RemoveSoftware(computerName) 
Catch ex As Exception 
    Call WriteLog (computerName & " lost connectivity while " & ex.Message) 
End Try 

을 그리고 이것은 PingOrFail 방법입니다 : 다음 코드 흐름은 다음과 같이 보일 수있는 프로그램에 메시지가 연결을 잃었 동안 액션과 일치하지 않는

Public Sub PingOrFail(computerName as String, message As String) 
    If My.Computer.Network.Ping(computerName) = False 
     Throw New Exception (message) 
    End If 
End Sub 
+0

감사합니다. 그러나 InstallVS를 시도한 다음 위의 테스트가 실패하면 PEC를 시도해보십시오. 언제든지 ping 검사가 실패하면 수행중인 작업을 중지하고 if 문을 종료해야합니다. – K20GH

+0

예외가 throw되면 (PingOrFail 메서드가 추가되어 Throw 된 위치를 표시 함) 실행이 해당 예외가 발견 된 첫 번째 위치 -이 경우 Exception Exception 문. WriteLog가 처리되면 그 시점부터 계속되며 예외가 발생한 지점으로 되돌아 가지 않습니다. –

+0

감사합니다. 나는 그것을 줄 것이다 – K20GH

2

이러한 문은 중첩 될 필요가 없으며 실패 할 경우 예외를 발생시킬 수 있습니다.

Private Sub DoStuff(ByVal computerName As String) 
    Try 
     If My.Computer.Network.Ping(computerName) Then 
      InstallVS(computerName) 
     Else 
      Throw New Exception(computerName & " lost connectivity while attemping to Install") 
     End If 
     If My.Computer.Network.Ping(computerName) Then 
      PEC(computerName) 
     Else 
      Throw New Exception(computerName & " lost connectivity while Forcing Communication") 
     End If 
     If My.Computer.Network.Ping(computerName) Then 
      RemoveSoftware(computerName) 
     Else 
      Throw New Exception(computerName & " lost connectivity while attemping to remove the temp software") 
     End If 
    Catch ex As Exception 
     WriteLog(ex.Message) 
    End Try 
End Sub 
+0

이 실행되는 동안 작업을 실행하기 전에 메시지 텍스트를 설정하기 때문에 "루프"가 종료됩니까? – K20GH

+2

"Throw New Exception"문이있는 행은 "Exception Exception"행과 WriteLog 문이있는 블록으로 이동합니다. ex.Message에는 사용자가 수행 한 텍스트가 포함됩니다. –

+0

이유는 알 수 없지만 그 출력은 모두 "실제 Ping 메시지가 아닌 예외가 Ping 요청 중에 발생했습니다" My.Computer.Network 인 경우에만 – K20GH