Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug Mode In VB 6?

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

#ifdef _DEBUG_
    // do things
#else
    // do other things
#end if
like image 750
Nahum Avatar asked Jan 29 '12 08:01

Nahum


People also ask

How do you Debug a visual code?

To bring up the Run and Debug view, select the Run and Debug icon in the Activity Bar on the side of VS Code. You can also use the keyboard shortcut Ctrl+Shift+D. The Run and Debug view displays all information related to running and debugging and has a top bar with debugging commands and configuration settings.

How do I Debug a breakpoint in Visual Studio?

To set a breakpoint in source code, click in the far left margin next to a line of code. You can also select the line and press F9, select Debug > Toggle Breakpoint, or right-click and select Breakpoint > Insert breakpoint. The breakpoint appears as a red dot in the left margin.


Video Answer


3 Answers

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.

like image 138
Cody Gray Avatar answered Sep 30 '22 13:09

Cody Gray


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)
like image 41
MarkJ Avatar answered Sep 30 '22 13:09

MarkJ


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.

like image 41
George Mastros Avatar answered Sep 30 '22 13:09

George Mastros