Creating your own custom constants file

Share your advanced PureBasic knowledge/code with the community.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Creating your own custom constants file

Post by PB »

If you use your own custom constants in a lot of your apps, and don't want to
have to declare them in each source, then just create your own "Residents"
file of them. Here's how:

(1) Create a new PureBasic source, called something like MyResidents.pb, and
put all your desired custom constants inside it, such as:

Code: Select all

#NetWork_Event_Failed = 0
#NetWork_Event_Success = 1
#PB_Window_Standard = 1 | 13107200 ; #PB_Window_ScreenCentered | #PB_Window_SystemMenu
You'll note one drawback: you can't use existing constants to declare your
new ones... which is why I've had to use "1 | 13107200" above, in place of
"#PB_Window_ScreenCentered | #PB_Window_SystemMenu". :( Oh well.

(2) Create a new batch file, in the same folder as your MyResidents.pb, called
MyResidents.bat, with its contents like this (not forgetting to change the paths
to your PureBasic installation folder as required):

Code: Select all

"C:\Program Files\PureBasic\Compilers\pbcompiler.exe" MyResidents.pb /resident MyResidents.res
move MyResidents.res "C:\Program Files\PureBasic\Residents\"
pause
(3) Double-click the batch file and that's it -- all your custom constants are
now instantly usable in all your sources! When you want to add more such
constants, just edit MyResidents.pb, save it, and run the batch file again. :)
Last edited by PB on Sun Oct 01, 2006 1:26 am, edited 1 time in total.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

Or use Tailbite ( just select the pb file )
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post by josku_x »

Good tip indeed!

I just made one resident file and it really is easier than adding more than 50 lines of enumerations :wink:
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

Very nice, thank you...

Then I could do something like this for example:

#MAX_MODULE_NAME32 = #MAXI_MOD_NAME32

???

can you do structures, enums, and procedures as constants also? I have some of these that I find I use a lot and it would be nice to be able to just use a constant instead of having to put the procedure in everything (of course i've converted some of them to libraries but i like the constant idea also)
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post by josku_x »

I don't think you can make a constant from a structure....
But, if you want to make enumerations as constants, then you have to convert them, like:

Code: Select all

Enumeration
  #Dummy
  #Dummy_1
EndEnumeration
to:

Code: Select all

#Dummy=0
#Dummy_1=1
And I think you can make procedures as constants, only if the procedure returns an integer.
Like:

Code: Select all

#TheWindowSize=WindowWidth()
Hope that helps.-.
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Post by freedimension »

josku_x wrote: And I think you can make procedures as constants, only if the procedure returns an integer.
Like:

Code: Select all

#TheWindowSize=WindowWidth()
Huh? :shock:
<°)))o><²³
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

josku_x wrote:

Code: Select all

#TheWindowSize=WindowWidth()
It is VESA only?
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

ts-soft wrote:
josku_x wrote:

Code: Select all

#TheWindowSize=WindowWidth()
It is VESA only?
It is "goldfish glass" with "goldfish water" extension only.
bye,
Daniel
Fred
Administrator
Administrator
Posts: 18249
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

josku_x wrote:Hope that helps.-.
It doesn't as the both informations are wrong.

1) Enumerations are available while building the residents, just try it.

2) Procedure aren't available in resident. And you can't affect a runtime procedure result to a constant at all...
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post by Dr. Dri »

Fred wrote:
josku_x wrote:Hope that helps.-.
It doesn't as the both informations are wrong.

1) Enumerations are available while building the residents, just try it.

2) Procedure aren't available in resident. And you can't affect a runtime procedure result to a constant at all...
Not even with procedures like Chr() ?
For example Chr(34) will always have the same value, it may be considered as a constant ?

Dri
Gansta93
Enthusiast
Enthusiast
Posts: 238
Joined: Wed Oct 20, 2004 7:16 pm
Location: The Village
Contact:

Post by Gansta93 »

Dr. Dri wrote:
Fred wrote:
josku_x wrote:Hope that helps.-.
It doesn't as the both informations are wrong.

1) Enumerations are available while building the residents, just try it.

2) Procedure aren't available in resident. And you can't affect a runtime procedure result to a constant at all...
Not even with procedures like Chr() ?
For example Chr(34) will always have the same value, it may be considered as a constant ?

Dri
With Chr(), it works. I have already tried that.

Code: Select all

#Copy = Chr(169)
works
Be seeing you! :-)

Gansta93
If you speak french, you can visite Le Monde de Gansta93 (Gansta93's World)
Fred
Administrator
Administrator
Posts: 18249
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Chr() is the expection, right :P
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post by josku_x »

Argh, dummy me :lol:
Didn't check the information if it's true or not....
Sorry
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post by Dr. Dri »

Fred wrote:Chr() is the expection, right :P
could it be more exceptions ?

Code: Select all

#PI    = ACos(0) * 2
#PI$   = StrF(PI, 17)
#Hello = Len("Hello")
#Color = RGB(1, 2, 3)
Dri
Straker
Enthusiast
Enthusiast
Posts: 701
Joined: Wed Apr 13, 2005 10:45 pm
Location: Idaho, USA

Post by Straker »

Fred wrote:Chr() is the expection, right :P
I read that as "expectation". I see now you mean "exception".

Before we close this off, Chr() is the exception, as long as you don't pass it a variable.

Code: Select all

#Copy = Chr(169)
works, however

Code: Select all

myVar.l = 169
#Copy = Chr(myVar.l)
Does not.

Chr() with an explicit argument gets pre-compiled out, just like a #Constant does, so don't use it to try to hide strings (i.e. passwords) in your EXEs because you will be sadly disappointed.

/learned the hard way.
Post Reply