Interpreted or Compiled?

It is highly unfortunate that Visual Basic doesn’t offer a way to execute a group of statements only when the program is interpreted in the environment or only when it has been compiled (indifferently as native code or p-code) to an EXE file. What we badly need is a conditional compilation constant that helps us to discern between the two execution modes, as in:

#Const DebugMode = -1#If DebugMode Then    ' code executed only within the environment#Else    ' code executed only within compiled programs#End If

Until Microsoft adds this feature to Visual Basic, we can use this function:

Function DebugMode() As Boolean    On Error Resume Next    Debug.Print 1 / 0    DebugMode = (Err <> 0)    Err.ClearEnd Function

This code works because Debug.Print statements are discarded when the code is compiled, therefore the error will only occur when the function is executed within the VB IDE. Note that, even with this workaround, a symbolic compilation constant is still required, for instance when you need alternate Declare or Dim statement that refer to the same function or variable, which cannot be put inside a regular If block without raising a compile-time error.

The problem of the above function is that it resets the error code. If you don’t want this side-effect, you can resort to the following function, which executes slightly slower (but only the first time it’s invoked) and whose logic is a bit more contorted:

Function DebugMode() As Boolean    Static Counter As Variant    If IsEmpty(Counter) Then        Counter = 1        Debug.Assert DebugMode() Or True        Counter = Counter - 1    ElseIf Counter = 1 Then        Counter = 0    End If    DebugMode = CounterEnd Function

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS