VB 6 中的调试模式?

发布于 2024-12-29 14:18:15 字数 128 浏览 0 评论 0原文

如何在 VB 6 中执行类似于以下 C 代码的操作?

#ifdef _DEBUG_
    // do things
#else
    // do other things
#end if

How can I do something similar to the following C code in VB 6?

#ifdef _DEBUG_
    // do things
#else
    // do other things
#end if

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

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

发布评论

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

评论(4

栖迟 2025-01-05 14:18:15

它与您习惯的其他语言几乎相同。语法如下所示:

#If DEBUG = 1 Then
    ' Do something
#Else
    ' Do something else
#End If

如果您只记得该语法与 VB 6 中的其他流程控制语句完全相同,那么就很容易记住,除了编译时条件以井号开头 (#< /代码>)。

诀窍实际上是定义 DEBUG (或其他)常量,因为我很确定默认情况下没有定义常量。有两种标准方法可以做到这一点:

  1. 使用#Const关键字在每个源文件的顶部定义常量。以这种方式建立的定义在整个源模块中都有效。它看起来像:

    <前><代码> #Const DEBUG = 1

  2. 在项目的属性中设置常量。这将定义一个在整个项目中有效的常量(并且可能是您想要的“调试”模式指示器)。

    为此,请在“Project Properties”对话框的“Make”选项卡上的“Conditional Compilation Constants”文本框中输入如下内容:

    <前><代码>调试 = 1

    您可以在此对话框中定义多个常量,方法是用冒号分隔每个常量 (:):

    <前><代码>调试 = 1 : 版本 2 = 1

请记住,假定任何定义的常量为 0。

It's pretty much the same as the other languages that you're used to. The syntax looks like this:

#If DEBUG = 1 Then
    ' Do something
#Else
    ' Do something else
#End If

It's easy to remember if you just remember that the syntax is exactly the same as the other flow-control statements in VB 6, except that the compile-time conditionals start with a pound sign (#).

The trick is actually defining the DEBUG (or whatever) constant because I'm pretty sure that there isn't one defined by default. There are two standard ways of doing it:

  1. Use the #Const keyword to define the constant at the top of each source file. The definition that you establish in this way is valid throughout the entire source module. It would look something like:

     #Const DEBUG = 1
    
  2. Set the constant in the project's properties. This would define a constant that is valid throughout the entire project (and is probably what you want for a "Debug" mode indicator).

    To do this, enter something like the following in the "Conditional Compilation Constants" textbox on the "Make" tab of the "Project Properties" dialog box:

     DEBUG = 1
    

    You can define multiple constants in this dialog by separating each of them with a colon (:):

     DEBUG = 1 : VERSION2 = 1
    

Remember that any constant which is not defined is assumed to be 0.

败给现实 2025-01-05 14:18:15

Cody 已经告诉您有关条件编译的信息。我想补充一点,如果您希望在 IDE 上调试时有不同的行为(例如,关闭您自己的错误处理以便 IDE 捕获错误),则您不需要条件编译。您可以像这样在运行时检测 IDE。

On Error Resume Next 
Debug.Print 1/0 
If Err=0 then 
  'Compiled Binary 
Else 
  'in the IDE 
End if

这是可行的,因为在编译的 EXE 中省略了 Debug.Print。

  • 编辑请记住关闭“错误时继续下一步”!
  • 编辑您可以将检查包装在像this这样的函数中(感谢CraigJ )

Cody has told you about conditional compilation. I'd like to add that if you want different behaviour when debugging on the IDE (e.g. turn off your own error handling so that the IDE traps errors) you don't need conditional compilation. You can detect the IDE at runtime like this.

On Error Resume Next 
Debug.Print 1/0 
If Err=0 then 
  'Compiled Binary 
Else 
  'in the IDE 
End if

This works because Debug.Print is omitted in the compiled EXE.

  • EDIT Remember to turn off On Error Resume Next !
  • EDIT You can wrap the check in a function like this (thanks CraigJ)
南城追梦 2025-01-05 14:18:15

要实现与 MarkJ 相同的效果,但具有错误处理功能,可以使用以下代码。

Public Function GetRunningInIDE() As Boolean

   Dim x As Long
   Debug.Assert Not TestIDE(x)
   GetRunningInIDE = x = 1

End Function

Private Function TestIDE(x As Long) As Boolean

    x = 1

End Function

当您从 IDE 中运行时,调用函数会产生额外的开销(这是非常小的)。编译时,其计算结果为简单的数字比较。

To achieve the same effect as MarkJ, but with error handling, you can use the following code.

Public Function GetRunningInIDE() As Boolean

   Dim x As Long
   Debug.Assert Not TestIDE(x)
   GetRunningInIDE = x = 1

End Function

Private Function TestIDE(x As Long) As Boolean

    x = 1

End Function

When you are running from within the IDE, there will be an extra overhead of calling a function (which is ridiculously small). When compiled, this evaluates to a simple number comparison.

慕巷 2025-01-05 14:18:15

这是我的简短而稳定的代码。我认为它比条件常量更好,因为您不需要每次复杂时都更改它。

Public Function InIDE() As Boolean
  On Error Resume Next
  Debug.Print 0 / 0
  InIDE = Err.Number <> 0
End Function

This is my short and stable code. I think it is better than conditional constants, because you don't need to change it every complication time.

Public Function InIDE() As Boolean
  On Error Resume Next
  Debug.Print 0 / 0
  InIDE = Err.Number <> 0
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文