How to use Compiler Custom Constants

Just starting out? Need help? Post your questions and find answers here.
TeraByte
User
User
Posts: 40
Joined: Wed May 09, 2012 12:40 am

How to use Compiler Custom Constants

Post by TeraByte »

I am trying to use the Compiler > Compiler Options > Constants > Custom Constants.

Here's what I tried:
1. Create a custom constant called #DebugLog and click "Add".
2. Now #DebugLog appears in the Custom Constants list.

In my code, I have

Code: Select all

CompilerIf #DebugLog
  CompilerError "Debug log is on"
CompilerElse
  CompilerError "Debug log is off"
CompilerEndIf
Compiler reports "Constant not found:#DebugLog."

So next I tried this:

Code: Select all

#DebugLog = 0

CompilerIf #DebugLog
  CompilerError "Debug log is on"
CompilerElse
  CompilerError "Debug log is off"
CompilerEndIf
This works fine. However, it apparently has nothing to do with my Custom Constant because I can delete it from the Compiler options and it still works.

So my question is how am I supposed to properly use the Compiler Custom Constant ? I was hoping to define it as a Compiler Custom Constant and then enable/disable it from the compiler options.

Thank you.
User avatar
STARGÅTE
Addict
Addict
Posts: 2255
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: How to use Compiler Custom Constants

Post by STARGÅTE »

Code: Select all

;#DebugLog = 1

CompilerIf Defined(DebugLog, #PB_Constant)
  CompilerError "Debug log is on"
CompilerElse
  #DebugLog = 0
  CompilerError "Debug log is off"
CompilerEndIf 

PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Little John
Addict
Addict
Posts: 4800
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: How to use Compiler Custom Constants

Post by Little John »

TeraByte wrote:I am trying to use the Compiler > Compiler Options > Constants > Custom Constants.

Here's what I tried:
1. Create a custom constant called #DebugLog and click "Add".
Assign a value of your choice to the constant in the IDE before clicking "Add":
#DebugLog = 1
andreasahlen
New User
New User
Posts: 7
Joined: Fri Jul 06, 2018 10:55 am
Location: Wasserburg am Inn (BY / DE)

Re: How to use Compiler Custom Constants

Post by andreasahlen »

Set your contant in Constants => Custom Constants in Compiler Options
#DEBUGMODE = 1
Eval constant value using:

Code: Select all

; Main
CompilerIf Defined(DEBUGMODE, #PB_Constant)
  Debug "DEBUG mode"
CompilerEndIf
-------
Regards | Saluti | Grüße
Andy
-------
Happy coding :)
User avatar
kenmo
Addict
Addict
Posts: 2049
Joined: Tue Dec 23, 2003 3:54 am

Re: How to use Compiler Custom Constants

Post by kenmo »

You can use either method.

1. You might be used to languages like C, where you can just #define DEBUGLOG and check if it's defined or not.
In PureBasic you can't just define a constant name, it must have a value (maybe the compiler should warn you!).
So use #DebugLog=1 or #DebugLog=0 then check:

Code: Select all

CompilerIf #DebugLog
OR 2. Define the constant #DebugLog=AnyValue and use the checkbox to enable or disable it, then check:

Code: Select all

CompilerIf Defined(DebugLog, #PB_Constant)
BarryG
Addict
Addict
Posts: 4211
Joined: Thu Apr 18, 2019 8:17 am

Re: How to use Compiler Custom Constants

Post by BarryG »

TeraByte wrote:Here's what I tried:
1. Create a custom constant called #DebugLog and click "Add".
Sounds like you didn't add a value to the constant -> https://i.imgur.com/vo4exWu.png
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: How to use Compiler Custom Constants

Post by Josh »

... 8 years later :mrgreen:
sorry for my bad english
BarryG
Addict
Addict
Posts: 4211
Joined: Thu Apr 18, 2019 8:17 am

Re: How to use Compiler Custom Constants

Post by BarryG »

I've bookmarked this forum with "View new posts", and this thread showed up. I don't look at the dates. andreasahlen was the one who revived it.
User avatar
kenmo
Addict
Addict
Posts: 2049
Joined: Tue Dec 23, 2003 3:54 am

Re: How to use Compiler Custom Constants

Post by kenmo »

:lol: I did not notice the age either, I just read "View active topics"
BarryG
Addict
Addict
Posts: 4211
Joined: Thu Apr 18, 2019 8:17 am

Re: How to use Compiler Custom Constants

Post by BarryG »

A lot of the time, someone will post a link to an older post too, and then someone will go there and read that, not realising it's 10+ years old. They might then reply to it, thus reviving it. No big deal, IMO. It adds value to old posts to keep them updated with relevant new info.
Post Reply