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.

you can use a with block, but that only allows one variable at a time, and doesn't nest intuitively. otherwise I won't say it doesn't exist, but i've been working with VB for a long time, and never seen any scope delimiters as you suggest.
–
Frank ThomasNov 15 '12 at 20:37

I gather the type for A and B changes for each proposed repeating code block? If so why would have want to keep the same names for the variables? If the type doesn't change per repeating code block, then why not just declare the variables at the top of the method and re-use them?
–
BlackMaelNov 15 '12 at 20:54

@BlackMael: The type stays the same, but I want the values to go out of scope to prevent them from being reused by the next block.
–
CarterNov 15 '12 at 20:56

Got me stumped. I'd probably go for the anwser by @EkoostikMartin but I am finding it difficult to understand that the code is too complex to break into subroutines but not too difficult to break into code blocks. Do you use CodeRush/Refactor, Resharper or something similar to do the refactoring for you?
–
BlackMaelNov 15 '12 at 21:04

@EkoostikMartin: The code is for a template that I am handing out to beginning programmers, so I can't rely on re factoring code. The code is complex because it references a bunch of resource file locations and typed datasets with long namespaces. The sub call got really big so I started creating variables to make it more readable.
–
CarterNov 15 '12 at 21:32

(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.