Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Interpreted or Compiled?

Total Hit ( 2706)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


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:

Click here to copy the following block
#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:

Click here to copy the following block
Function DebugMode() As Boolean
  On Error Resume Next
  Debug.Print 1 / 0
  DebugMode = (Err <> 0)
  Err.Clear
End 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:

Click here to copy the following block
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 = Counter
End Function


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.