条件编译问题
Most everyone who has done any programming in VB6 knows that you can do something in code like
大多数在VB6中进行过任何编程的人都知道,您可以在代码中执行某些操作,例如
Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Assert which when used in a line of code like Debug.Assert MyBoolean will cause a break in the program at that line if MyBoolean is True. Both Debug statements will be ignored when the program is compiled so they are superior to MsgBox statements which need to be removed before a program is compiled, unless, of course, you want the user to know the value of MyVar.
Debug.Print MyVar ,并且当程序从IDE运行时,MyVar的值将显示在立即窗口中。 不太为人所知的是Debug.Assert,如果将其用于Debug.Assert MyBoolean之类的代码行中,则如果MyBoolean为True,则会导致该行的程序中断。 编译程序时,两个Debug语句都将被忽略,因此它们优于MsgBox语句,后者在编译程序之前需要删除,除非您希望用户知道MyVar的值。
Debug statements are useful but limited However there is another debugging method that is less well known and that is conditional compilation. Conditional compilation uses special #!f, #Else, #ElseIf and #End If reserved words that like Debug are not included in compiled code. This can be very handy in debugging where you can do something like
调试语句是有用的,但有局限性。但是,还有另一种不太为人所知的调试方法,即条件编译。 条件编译使用特殊的#!f,#Else,#ElseIf和#End If保留字(例如Debug)不包含在编译代码中。 这在调试中非常方便,您可以在其中执行以下操作
#If MyVar > 5 Then ' or the value of some Boolean is True, etc.
' Do something (or any number of things)
#Else
' Do something else
#End If
I find it particularly useful when doing things like that to set up a 'Conditional Compilation Argument' by going to Project|<MyProgramName|Pro
我发现执行诸如此类的操作时,通过转到Project | <MyProgramName | Pro来设置“条件编译Testing = -1 ”之类的内容。 请注意,-1或任何非零值都等于True,并且条件编译参数具有全局作用域。 换句话说,它们在程序中的任何位置都可用。 完成此操作后,我可以执行以下操作,这是我的一个应用程序中的代码。
#If Testing Then
picSmilie(0).Visible = True
picSmilie(picSmilie.Count - 1).Top = picSmilie(picSmilie.Count - 2).Top - 40
picSmilie(picSmilie.Count - 1).Visible = True
#End If
You don't have to understand exactly what that does except to know that it alters what appears in the program when I'm testing it.
您不必确切地了解它的作用,只需要知道它会改变我测试程序中的内容即可。
It's important to realize that unless Testing is changed to 0 (False) before compilation that that code will be compiled into the program and that is probably not what you want. There's an easy fix for that however and that is to add a Sub like the following.
重要的是要意识到,除非在编译之前将Testing更改为0(False),否则该代码将被编译到程序中,而这可能不是您想要的。 有一个简单的解决方法,那就是添加一个Sub,如下所示。
Public Sub DoNotAllowTestCodeToCompile()
#If Testing Then
Do not compile
#End If
End Sub
If Testing is left True then the #If/#Else the compiler will stop with an error since Do not compile is not a valid line of code:) If you have already changed Testing to 0 then the code in the Sub will be ignored and barring other errors the code will compile.
如果将Testing设置为True,则#If /#Else编译器将因错误停止,因为不编译不是有效的代码行:)如果已经将Testing更改为0,则Sub中的代码将被忽略,并且除非存在其他错误,否则代码将编译。
One final note and that is that while conditional compilation can be a valuable testing tool, there are uses for it that don't involve testing at all like in this explanation and code from the MSDN Library.
最后一点要注意的是,尽管条件编译可以是一种有价值的测试工具,但它的用途根本不涉及测试,如本说明和MSDN库中的代码。
#If conFrenchVersion Then
#If conFrenchVersion然后
' <code specific to the French language version>.
'<特定于法语版本的代码>。
#ElseIf conGermanVersion then
#Else如果conGermanVersion那么
' <code specific to the German language version>.
'<特定于德语版本的代码>。
#Else
#其他
' <code specific to other versions>.
'<其他版本专用的代码>。
#End If
#万一
If the value of the conFrenchVersion constant is set to True at compile time, the conditional code for the French language version will be compiled. If the value of the conGermanVersion constant is set to True, the compiler uses the German language version.
如果conFrenchVersion常量的值在编译时设置为True,则将编译法语版本的条件代码。 如果conGermanVersion常量的值设置为True,则编译器将使用德语版本。
If you find that this article has been helpful, please click the “thumb’s up” button below. Doing so lets me know what is valuable for EE members and provides direction for future articles. It also provides me with positive feedback in the form of a few points. Thanks!
如果您发现本文对您有所帮助,请单击下面的“竖起大拇指”按钮。 这样做可以让我知道对EE成员有价值的内容,并为以后的文章提供指导。 它还以几点的形式为我提供了积极的反馈。 谢谢!
翻译自: https://www.experts-exchange.com/articles/8809/Conditional-Compilation.html
条件编译问题