如何在 VB.NET 中使用 #IF DEBUG

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3613249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:40:17  来源:igfitidea点击:

How to use #IF DEBUG in VB.NET

.netvb.net

提问by GregH

Is it possible to assign a value to a class variable from inside a #IF DEBUGconditional?

是否可以从#IF DEBUG条件内部为类变量赋值?

I want to conditionally execute some code from inside my main form load if I am running in DEBUGmode. I thought I could do something like:

如果我在DEBUG模式下运行,我想有条件地从我的主表单加载中执行一些代码。我以为我可以做这样的事情:

Public Class Form1
    public DEB as Integer

    #if DEBUG then
        DEB = 1
    #else
        DEB = 0
    #end if

    Private Sub Form1_Load(....)
        if DEB=1 Then
            <do something>
        else
            <do something else>
        end if
    ....

However, it seems like you can't assign a value to a variable. I'm obviously not understanding the scoping correctly. I can't seem to put the #if DEBUGinside the Load sub routine. How do I do this?

但是,您似乎无法为变量赋值。我显然没有正确理解范围。我似乎无法将#if DEBUGLoad 子例程放在里面。我该怎么做呢?

回答by Christian Hayter

Why not just test the compilation constant directly? You are not gaining anything by testing an actual variable.

为什么不直接测试编译常量呢?通过测试实际变量,您不会获得任何收益。

Public Class Form1

Private Sub Form1_Load(....)

#if DEBUG then
    <do something>
#else 
    <do something else>
#end if

End Sub

End Class

回答by SteveCinq

I don't see your problem here. I do this and it works fine. It's a bit annoying that the compilation constant isn't available directly to running code but it makes sense when you think about it.

我在这里看不到你的问题。我这样做并且效果很好。编译常量不能直接用于运行代码,这有点烦人,但是当您考虑它时它是有道理的。

In response to Christian, I think the case against just enclosing your code in the compiler directives is that with a code variable you can write cleaner, more descriptive code with potentially less duplication and therefore simpler maintenance.

作为对 Christian 的回应,我认为反对仅将您的代码包含在编译器指令中的理由是,使用代码变量,您可以编写更清晰、更具描述性的代码,并且可能会减少重复,从而简化维护。

For example, here's my standard library code:

例如,这是我的标准库代码:

Public Module Common

    #if DEBUG then
        Public In_Debug As Boolean = True
    #else
        Public In_Debug As Boolean = False
    #end if
End Module

Public Class Form1

    Private Sub Form1_Load(....)

        If In_Debug Then SplashScreen.Hide()
        ...

Placing the In-Debugcode variable in a public module effectively makes it a Globalwhich you can treat as a constant throughout your project.

In-Debug代码变量放在公共模块中有效地使其成为全局变量,您可以将其视为整个项目中的常量。

(Example explanation: I don't show the splash screen in debug because it may hide dialogs, etc which can stop you progressing.)

(示例说明:我不会在调试中显示启动画面,因为它可能会隐藏对话框等,这会阻止您继续进行。)

Obviously, you can do the same thing with the TRACE constant or any custom compiler constant you choose to declare.

显然,您可以对 TRACE 常量或您选择声明的任何自定义编译器常量执行相同的操作。

And, yes, as GregH mentioned, you need to make sure that you actually have a DEBUG constant declared in your project's Debugconfiguration (whether or not it's actually called "Debug"). You don't need it declared in Release; it's absence is interpreted as "False".

而且,是的,正如 GregH 提到的,您需要确保在项目的Debug配置中确实声明了一个 DEBUG 常量(无论它是否实际上称为“Debug”)。您不需要在Release 中声明它;它的缺席被解释为“假”。