devxlogo

Debug Print in an EXE

Debug Print in an EXE

Contrary to common belief, Debug.Print statements are not always removed from an executable. This behavior can be demonstrated easily. Open a new project, place a single CommandButton on the default form, and add this code:

 Public Function DebugTime()	MsgBox "Caught!"End FunctionPrivate Sub Command1_Click()	Debug.Print DebugTimeEnd Sub

Compile the program to an EXE, run, and click on the button. Unexpectedly, the message box appears. This is obviously a contrived example, but it’s easy to imagine cases where Debug.Print is used to print the return value of a function, for example. If variables are passed as ByRef parameters, and if the function alters the value of those variables, this error is propagated through to the executable, and might be extremely difficult to find. The morals of this tip are:
1. Pass parameters ByVal unless you’re certain you’re not going to change them, or you’re using them as “output” parameters.
2. Be careful when using Debug.Print. It might be doing more than you think.

devx-admin

Share the Post: