2013-11-04 18 views
0

我有類似的代碼是:退出Using塊

Using conn As OdbcConnection = getNewConnection() 

'' Some code 

If breakFlag Then 
    canExit = True 
    GoTo exitUpdate 
End If 

exitUpdate: 
End Using 

我想擺脫goto語句。
因爲這將有得心應手的ExitUsing命令,但我們沒有。

這裏是退出的一些好方法在類似的情況下使用塊或我必須以不同的方式設計我的代碼?

+1

「使用」模塊並不像「While」或「For」循環那樣是一個真正的控制結構。它只是'Dispose'模式的包裝器。因此,可能有也可能沒有理由在「Using」塊之外執行任何操作,具體取決於您的代碼應該執行的操作。 –

回答

2
Using conn As OdbcConnection = getNewConnection() 
    '' Some code 

    If breakFlag Then 
     canExit = True 
     ' remove this: GoTo exitUpdate 
    ELSE 
     ' here place the rest of your logic that would execute when breakflag = false 
    End If 
End Using