vb.net artificial scope

.net, c#, c#-to-vb.net, syntax, vb.net

Solution

I found this answer on a Japanese site.

With Nothing
  Dim A = ...
  Dim B = ...
  (Code using A & B)
End With

from http://www.ilovex.co.jp/Division/ITD/archives/2007/11/with_nothing.html

(Translated from Japanese) I think if we think about it, to use a With statement of whether it is not a better. Because, With statement is intended to be used in order to omit the code, because it is not intended to associative processing from the statement itself.

So the point is, the "With" statement doesn't really do anything other than shortcut contained code. It does, however, create a unique scope. This seems like it should be pretty light in the compiler. While I would love to see some new statement like "Begin/End" this is what I will use from now on.

Problem

In VB.Net 4.0, I have situations where I need to repeat similar code, but due to the complexity of the code I won't be able to create subroutines to simplify. I want to be able to declare variables in a block scope, go out of scope, then repeat. My current method is to do something like this. ``` If True Then Dim A = ... Dim B = ... (Code using A & B) End If ``` ...And then repeat as much as I need. This one works too... ``` Try Dim A = ... Dim B = ... (Code using A & B) Finally End Try ``` Otherwise I have to give each variable unique names... ``` Dim A1 = ... Dim B1 = ... (Code using A1 and B1) Dim A2 = ... Dim B2 = ... (Code using A2 and B2) ``` ...which makes repeating the code dangerous because I may forget to change a variable name as I am copying code. You can do it in C#... ``` { int A = ... int B = ... (Code using A & B) } ``` Is there a way in VB.Net to create a block this way?

Original source