I'm writing a code and I have a situation like this:
Sub Example()
Using DT As New DataTable
'...some code
If mCondition = True Then GoTo Other
'...some code
End Using
Other:
'...some code
End Sub
I'm worried about the consequences of "goto" on the block "Using": does it work correctly?
Do I need to use a different code structure? (or is better if I do so)
You don't have to worry about existing out of a Using block because the compiler will handle the disposing of resources as soon as control flow moves out of the block.
From The documentation:
"A Using block behaves like a Try...Finally construction in which the Try block uses the resources and the Finally block disposes of them. Because of this, the Using block guarantees disposal of the resources, no matter how you exit the block. This is true even in the case of an unhandled exception, except for a StackOverflowException."
So your code is effectively behaving like this:
Sub Example()
Dim DT As New DataTable
Try
'...some code
If mCondition = True Then GoTo Other
'...some code
Finally
DT.Dispose
End Try
Other:
'...some code
End Sub
Having said that, having a goto in the first place is almost always a code smell, so you could do with refactoring in some way. See: GoTo statements and alternatives in VB.NET