Help with Compiler Directives and Vars

Hello all,

I have the following code sprinkled throughout my project. The purpose is to print information only when either the project is in debug build OR a variable Debug = True. That was all good until I decided that I wanted to control the App.Debug var which was a constant.

#If DebugBuild Or App.Debug = True Then
  Print "TRACE "+ CurrentMethodName  +  "  Start" 
#EndIf

Once I change debug to a property it will no longer compile since anything in the Compiler Directive (#If) MUST be a constant.

So I can either replace the code

#If DebugBuild Or App.Debug = True Then

#Endif

to simply:

If App.debug = True Then

Is there another way to do this without loosing the compiler directive??? So far, I have not found a good way…

Thanks
Tim

You must use constants with compiler directives because the calculation is performed once at compile time. A property or variable is evaluated at runtime and thus cannot control what the compiler builds.

You must either use a constant, or a runtime evaluation as you had discovered.

1 Like

Figured as much Tim. Was just hoping that someone smarter than I had figured out a way!

Thanks for your response!
Tim

@Tim_Seyfarth - you can set constants in pre-build steps though and you can ask questions with message dialogs so I bet you can make this work.

You can use a regular If statement with a compiler directive, e.g.

If DebugBuild Or App.Debug = True Then
  Print "TRACE "+ CurrentMethodName  +  "  Start" 
End If

An easier way to do it (IMHO) is to write a function:

sub DebugLog(message as string)
  if DebugBuild or App.Debug then
     Print(message) 
  end if
end sub

So your code becomes much cleaner:

function foobar()
   DebugLog("starting " + currentMethodName )

   [... other code ...]

   DebugLog("finished " + currentMethodName)
end function

Thank you guys!
I appreciate the feedback. I’ll be circling back to this in a week or so…

Tim