Page 1 of 2

Creating your own custom constants file

Posted: Sun Jan 22, 2006 12:42 pm
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. :)

Posted: Sun Jan 22, 2006 1:33 pm
by Droopy
Or use Tailbite ( just select the pb file )

Posted: Sun Jan 22, 2006 2:38 pm
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:

Posted: Sun Jan 22, 2006 4:13 pm
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)

Posted: Sun Jan 22, 2006 5:04 pm
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.-.

Posted: Sun Jan 22, 2006 5:14 pm
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:

Posted: Sun Jan 22, 2006 5:56 pm
by ts-soft
josku_x wrote:

Code: Select all

#TheWindowSize=WindowWidth()
It is VESA only?

Posted: Sun Jan 22, 2006 6:18 pm
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.

Posted: Sun Jan 22, 2006 7:40 pm
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...

Posted: Sun Jan 22, 2006 9:09 pm
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

Posted: Sun Jan 22, 2006 9:49 pm
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

Posted: Sun Jan 22, 2006 11:26 pm
by Fred
Chr() is the expection, right :P

Posted: Mon Jan 23, 2006 8:41 am
by josku_x
Argh, dummy me :lol:
Didn't check the information if it's true or not....
Sorry

Posted: Mon Jan 23, 2006 9:27 am
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

Posted: Mon Jan 23, 2006 9:31 am
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.