Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code regions not allowed within method bodies in VB.NET?

Note: This "feature" has now been added to Visual Studio 2015 but the question will hold a while since not every developer or every dev shop gets access to the latest and greatest IDE as soon as it comes out.

ORIGINAL QUESTION:

Normally I wouldn't "need" or even consider a ridiculous feature such as code regions within method bodies but: I'm refactoring VB.NET code where methods routinely run five hundred lines of code or more and the references are so tightly coupled that the code defies simple refactoring such as method extraction.

And that's why I thought I would try regions within a method body. I just wanted to organize the code for the short term. But the IDE doesn't let me (resulted in a compiler error.) I'm just curious as to why? Seems like code regions shouldn't impact the compiler, intellisense etc. Am I missing something? (Still using VS 2005 btw.)

Interesting: This seems to be language specific. It's OK in C# (I didn't check that initially) but not in VB.NET.

public module MyModule     Sub RunSnippet()         dim a as A = new A (Int32.MaxValue )          #region          Console.WriteLine ("")         #end region        .... 

that gets a compiler error but the C# version is ok.

like image 552
Paul Sasik Avatar asked Jun 30 '10 17:06

Paul Sasik


People also ask

What is #region in VB net?

Use the #Region directive to specify a block of code to expand or collapse when using the outlining feature of Visual Studio IDE. You can place, or nest, regions within other regions to group similar regions together.

What is #region in asp net?

It lets you specify a block of code that you can expand or collapse when using the outlining feature of the Visual Studio Code Editor. It should be terminated with #endregion. Let us see how to define a region using #region. #region NewClass definition public class NewClass { static void Main() { } } #endregion.

What do regions do in C#?

Regions in C# is a useful feature that helps manage code in areas that can be hidden or visible. Learn how to use regions in Visual Studio to improve code readability.


2 Answers

I think code regions probably wouldn't be supported in method bodies since they, as you put it, would be a (somewhat) "ridiculous feature" - However, in C#, this does work, at least in VS 2008 and VS 2010 - just not in VB.NET.

That being said, I would avoid it. Putting regions within a method body would just leads to people making larger methods (since that's the only time when it would be worthwhile), which is something that should be avoided, not encouraged.

If your code:

defies simple refactoring such as method extraction

I would focus, instead, on doing "complex" refactoring (or whatever it takes) to try to break up those methods. There is no way your "four or five hundred lines" long methods are at all maintainable in their current state.

Personally, I would leave them causing "pain" - make it obvious that they need work, right front and center, until you can break them up and refactor out portions.

like image 103
Reed Copsey Avatar answered Sep 26 '22 01:09

Reed Copsey


Another simple alternative method:

What you can do is basically select the code you want to add the #Region #End Region and basically click:

Ctrl+M, Ctrl+H

This basically wraps the code. And then to make it even neater and easy to find you can comment the code. The end result would look something like this:

enter image description here

like image 39
Zer0 Avatar answered Sep 22 '22 01:09

Zer0