VB.NET 中的方法体内不允许有代码区域?

发布于 2024-09-07 16:10:47 字数 683 浏览 4 评论 0原文

注意:此“功能”现已添加到 Visual Studio 2015 中,但这个问题将持续一段时间,因为并不是每个开发人员或每个开发商店都能在最新、最好的 IDE 发布后立即使用它。

原始问题:

通常我不会“需要”,甚至不会考虑一个荒谬的功能,例如方法体内的代码区域,但是:我正在重构 VB.NET 代码,其中方法通常运行 500 行代码或更多,并且引用如此紧密地耦合,以至于代码无法进行简单的重构,例如方法提取。

这就是为什么我想尝试方法体内的区域。我只是想短期组织代码。但 IDE 不允许我这样做(导致编译器错误)。我只是好奇为什么?似乎代码区域不应该影响编译器、智能感知等。我错过了什么吗? (顺便说一句,仍在使用 VS 2005。)

有趣:这似乎是特定于语言的。在 C# 中没问题(我最初没有检查),但在 VB.NET 中不行。

public module MyModule
    Sub RunSnippet()
        dim a as A = new A (Int32.MaxValue )

        #region 
        Console.WriteLine ("")
        #end region
       ....

会出现编译器错误,但 C# 版本没问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

过度放纵 2024-09-14 16:10:47

我认为方法体中可能不支持代码区域,因为正如您所说,它们将是一个(有点)“荒谬的功能” - 但是,在 C# 中,这确实至少有效在 VS 2008 和 VS 2010 中 - 只是不在 VB.NET 中。

话虽这么说,我会避免它。将区域放入方法体内只会导致人们制作更大的方法(因为这是唯一值得的时候),这是应该避免的事情,而不是鼓励的事情。

如果你的代码:

无法进行简单的重构,例如方法提取

相反,我将重点放在进行“复杂”重构(或任何需要做的事情)上,以尝试分解这些方法。您的“四五百行”长方法在当前状态下根本无法维护。

就我个人而言,我会让它们造成“痛苦”——让它们明显需要工作,在前面和中间,直到你可以分解它们并重构部分。

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.

握住我的手 2024-09-14 16:10:47

另一个简单的替代方法:

您可以做的基本上是选择要添加 #Region #End Region 的代码,然后单击:

Ctrl+M, Ctrl+H

这基本上包装了代码。然后,为了使其更加整洁和易于查找,您可以对代码进行注释。最终结果如下所示:

在此处输入图像描述

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

奈何桥上唱咆哮 2024-09-14 16:10:47

Visual Basic 9.0 语言规范第 3.3 章明确指出:

区域指令对源代码行进行分组,但对编译没有其他影响。整个组可以在集成开发环境 (IDE) 中折叠和隐藏,或展开和查看。 这些指令的特殊之处在于它们既不能在方法体内启动也不能终止

或者换句话说:你不能这样做,因为规范是这么说的。

至于为什么这样指定,我认为这与VB自有记忆起就具有的古老IDE功能有关:工具+选项、文本编辑器、基本、VB特定、显示程序行分隔符。这只是一种猜测,可能不是一个很好的猜测。


更新:现在由 Roslyn 支持,首先包含在 VS2015 中。

It is explicit in chapter 3.3 of the Visual Basic 9.0 Language Specification:

Region directives group lines of source code but have no other effect on compilation. The entire group can be collapsed and hidden, or expanded and viewed, in the integrated development environment (IDE). These directives are special in that they can neither start nor terminate within a method body

Or in other words: you cannot do it because the specification says so.

As to why it was specified like this, I reckon it has something to do with the age-old IDE feature that VB has had for as long as I can remember: Tools + Options, Text Editor, Basic, VB Specific, Show procedure line separators. That's just a guess, probably not a very good one.


Update: now supported by Roslyn, included first with VS2015.

戏剧牡丹亭 2024-09-14 16:10:47

截至2015 年 11 月:Visual Studio 2015 中现已支持它,只需执行您喜欢的操作即可。

示例代码:

With frmMain
#region "A sample region in odd place"
  .show()
  .text = "Sample"
#end region
End With

注意:在 Visual Studio 的早期版本中,它似乎无法在 VB.NET 中运行,但在 C# 中它可以工作。

As of November 2015: In Visual Studio 2015 it is now supported, just do what you like.

Sample Code:

With frmMain
#region "A sample region in odd place"
  .show()
  .text = "Sample"
#end region
End With

Note: In earlier versions of Visual Studio it seems that it is not working in VB.NET, but in C# it is working.

烏雲後面有陽光 2024-09-14 16:10:47

我不了解 VB,但据我所知,在 C# 中从 1.0 开始就允许这样做。

事实上,您甚至可以将代码区域放置在跨作用域的奇怪位置。例如:

class Test
{
    static void Main()
    {
        if (DateTime.Now.Hour > 12)
        {
#region Foo
            Console.WriteLine("Afternoon");            
        }
#endregion
    }
}

这里的区域开始if语句内,但结束在它之外。可怕的是,但编译器对此没问题。

当您说 IDE 没有“让”您将代码放入区域时,您的意思是什么?您收到编译器错误了吗?

I don't know about VB, but in C# this has been allowed since 1.0 as far as I'm aware.

Indeed, you can even put code regions in odd places which cross scopes. For example:

class Test
{
    static void Main()
    {
        if (DateTime.Now.Hour > 12)
        {
#region Foo
            Console.WriteLine("Afternoon");            
        }
#endregion
    }
}

Here the region starts within the if statement, but ends outside it. Horrible, but the compiler is fine with it.

What do you mean when you said the IDE didn't "let" you put code in regions? Did you get a compiler error?

┈┾☆殇 2024-09-14 16:10:47

这只是 VB 团队在将区域功能添加到 Visual Basic 语言版本 7 中时做出的选择。它被视为对于在声明级别进行组织而不是在方法内部有用的功能,因此仅在该级别允许。

C# 团队对此功能有不同的看法,并在许多其他地方允许它。我总是对 C# #region 指令可以出现在不同的声明上下文中感到惊讶。

#region Foo
class Bar {
#endregion

}

VB 中不允许使用等效代码。

This was simply a choice the VB team made when adding the regions feature into version 7 of the Visual Basic language. It was seen as a feature which was useful for organizing at a declaration level and not inside a method and hence was allowed only at that level.

The C# team felt differently about this feature and allow it in many other places. I've always found it surprising that C# #region directives can occur in different declaration contexts.

#region Foo
class Bar {
#endregion

}

The equivalent code is not allowed in VB.

如日中天 2024-09-14 16:10:47

对于任何寻找此问题最新答案的人来说,现在可以在 VB.NET 中实现这一点(从 版本 14 上)。

方法体内的区域指令

您可以将 #Region…#End Region 分隔符放置在文件中、函数内部甚至跨函数体中的任何位置。

OP 中的示例现在是完全合法的语法:

Sub RunSnippet()
    Dim a as A = New A (Int32.MaxValue )

    #Region "Test"
    Console.WriteLine ("")
    #End Region
End Sub

For anyone looking for the most recent answer to this question, this is now possible in VB.NET (starting from version 14 on).

Region Directives inside Method Bodies

You can put #Region…#End Region delimiters anywhere in a file, inside functions, and even spanning across function bodies.

The example from the OP is now perfectly legal syntax:

Sub RunSnippet()
    Dim a as A = New A (Int32.MaxValue )

    #Region "Test"
    Console.WriteLine ("")
    #End Region
End Sub
满身野味 2024-09-14 16:10:47

Visual Studio 2003 为 VB.NET 提供了这些功能,但该功能在 Visual Studio 2005 及更高版本中被删除。重构大型程序时确实很烦人,但您可以拆分代码窗口。

老实说,我希望 C# 能够限制区域的使用,因为它们被过度使用。我有一个宏,可以在继承 C# 项目时将它们从所有代码文件中删除。

删除的另一个功能是导航栏中的可重写方法列表。我检查了自 2005 年以来他们是否为每个新版本的 Visual Studio 重新添加了此功能。

Visual Studio 2003 had them for VB.NET, but feature was removed in Visual Studio 2005 and later. Really annoying when refactoring large procedures, yet you can split the code window.

Honestly, I wish C# would restrict region use because they are overly used. I have a macro that strips them out of all code files when inheriting C# projects.

Another feature removed was the list of overridable methods in the Navigation Bar. I check to see if they re-added this feature for every new version of Visual Studio since 2005.

你的心境我的脸 2024-09-14 16:10:47

当有人为此苦苦挣扎时,我使用了以下解决方案,因为我在重构时无法选择升级应用程序:

If 1 = 1 then 'Region "some name"
'这里有一些代码
End If

then 只是折叠 if 。

As someone struggling with this, I used the below solution since I don't have the option to upgrade the app while refactoring:

If 1 = 1 Then 'Region "some name"
'some code here
End If

Then just collapse the if.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文