How to create constants automatically?

Just starting out? Need help? Post your questions and find answers here.
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

How to create constants automatically?

Post by charvista »

Hello,
I know that PB cannot define (compose) constants natively by a variable or a function.
But I believe there must be an indirect(?) way (through API, ASM, or via a Macro)?
Who can help me? (if there ever exists a workaround for that)...
Here is where I started to define constants for my colors.

Code: Select all

EnableExplicit
Define.i R,G,B,N

Macro const(cvar,R,G,B)
    N=RGB(R,G,B)
    cvar=N
EndMacro

R=$FF
G=$B2
B=$00

const(#MYCOLOR,R,G,B)   ; should result in #MYCOLOR=$FFB200 (16757248)

Debug #MYCOLOR
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
NicTheQuick
Addict
Addict
Posts: 1527
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: How to create constants automatically?

Post by NicTheQuick »

There is no other way except with macros. Constants and macros are processed in the pre-compiler. And I don't know what API do you mean. Purebasic has no API you could use. And even Assembler will not help, especially if you use the C-Backend. :wink:

Can you please explain in more detail what you want to achieve?
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
AZJIO
Addict
Addict
Posts: 2226
Joined: Sun May 14, 2017 1:48 am

Re: How to create constants automatically?

Post by AZJIO »

Code: Select all

EnableExplicit
Define.i R,G,B,N

#MYCOLOR = $FFB200

R=Red(#MYCOLOR)
G=Green(#MYCOLOR)
B=Blue(#MYCOLOR)
cvar=#MYCOLOR
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: How to create constants automatically?

Post by charvista »

@AZJIO
It is the opposite that I am trying to do: the values R, G and B are normally read from a file, and from there I would like to put them in a constant. It is the automatic creation of the constants that I would like to do. Doing #MYCOLOR=$FFB200 is of course no problem, because there is no variable involved, it is #MYCOLOR=RGB(R,G,B) but that is not possible because constants cannot be created from variables... hence my question :wink:
In other words: not cvar=#MYCOLOR but #MYCOLOR=cvar

@NicTheQuick
If a macro is the only way, that's fine, but I would like to know how to do that. My attempt has failed. :?
I want to achieve #MYCOLOR=RGB(R,G,B)
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
mk-soft
Always Here
Always Here
Posts: 6320
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: How to create constants automatically?

Post by mk-soft »

Code: Select all

EnableExplicit

Macro const(_cvar,_R,_G,_B)
    _cvar= (_B << 16 | _G << 8 | _R)
EndMacro

#R=$FF
#G=$B2
#B=$00

const(#MYCOLOR,#R,#G,#B)   ; should result in #MYCOLOR=$FFB200 (16757248)

Debug Hex(#MYCOLOR)

Debug Red(#mycolor)
Last edited by mk-soft on Sun Apr 07, 2024 2:17 pm, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
AZJIO
Addict
Addict
Posts: 2226
Joined: Sun May 14, 2017 1:48 am

Re: How to create constants automatically?

Post by AZJIO »

@charvista
How is a constant different from a variable? As a result, this will be a memory cell, with the only difference being that the variable can be changed.
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: How to create constants automatically?

Post by charvista »

@mk-soft
#R=$FF
#G=$B2
#B=$00
You changed my variables into constants here.
R, G, B are here as variables. (R, G and B are values got from a file, but to make the example shorter, they are "already" in variables...)
Your code is nice, but then you should first do #R=R :mrgreen: which makes it a vicious circle.

@AZJIO
Constants are different from variables as they are global. My software is a "package", so it calls many procedures, but some colors must be equal the same everywhere. Okay, I can also use Global _MyColor = RGB(R,G,B) but I am trying to load them in constants as they cannot be modified later, once defined.

== > The only thing I am trying to accomplish, is to define a constant with the contents of a variable with a Macro.
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
Demivec
Addict
Addict
Posts: 4281
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: How to create constants automatically?

Post by Demivec »

charvista wrote:== > The only thing I am trying to accomplish, is to define a constant with the contents of a variable with a Macro.
The limitations of using variables and functions in the definition of constants at compile time is clear. Why do you want to define these values at compile time as individual constants? How are the constants intended to be used later in the code? Is the goal to save space, reduce errors, or what exactly?

It seems as if your data may be variable because you would otherwise just use numeric values in defining the constants.
You could define a table or map whose values are indexed and accessed as with constants representing the indexes or map key associations. If using a table you could just include the properly formatted color data from the file into the DataSection and use an accessory function to read the values. I'll work up an example if needed later when i get a moment. It all depends on your more specific needs.
User avatar
NicTheQuick
Addict
Addict
Posts: 1527
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: How to create constants automatically?

Post by NicTheQuick »

If you read that file with the RGB values at runtime there is no way to define constants. Constants are not there anymore during runtime. They are replaced with their value before compiling.
What you can do however is using Maps.

Code: Select all

; Using Longs here as they are big enough for RGB values
Global NewMap constants.l()

Macro DQ
	"
EndMacro

Macro c(name)
	constants(DQ#name#DQ)
EndMacro

; Main program

c(MYCOLOR) = RGB(1, 2, 3)
Debug c(MYCOLOR)
Maybe that's a solution you like.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: How to create constants automatically?

Post by breeze4me »

That's not possible with a numeric constant, but it is with a string constant.

Code: Select all

#MyColor$ = "A unique string that is never used in the code."
*p = @"A unique string that is never used in the code."

R=$00
G=$B2
B=$FF

PokeS(*p, Str(RGB(R,G,B)))

color.l = Val(#MyColor$)
a.s = #MyColor$

Debug Hex(color, #PB_Long)    ; FFB200
Debug a          ;16757248
User avatar
STARGÅTE
Addict
Addict
Posts: 2260
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: How to create constants automatically?

Post by STARGÅTE »

A constant is just a macro with a hashtag. Once defined, it is replace everywhere during the compilation and is not present anymore in the final executable. I think at this point the discussion is closed.

When you want to define a constant from another file (during compilation), you can use IncludeFile.
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
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: How to create constants automatically?

Post by charvista »

@Demivec, Stargåte, NicTheQuick
Allright, I understand now what is happening under the hood of PB when compiling the program and that they are no longer modifiable afterwards.

@breeze4me
Fascinating :shock:
So I only need to use Val() or ValD() to get its numeric value.
But finally, I think I will use Global variables, it is much easier.

@Demivec (again :) )
Your example with DataSection is very welcome, I am a bit wondering how you would do that using a table.
To make it easy to understand, it is like reading in a file the colors that were chosen in a "Colors settings" program.
For example, the background color, the title color, and other elements colors are configured there.
It is like chosing for a light theme, or for a dark theme.
Then, I want to load them in "constants", and all programs use them accordingly.
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: How to create constants automatically?

Post by charvista »

@NicTheQuick
I studied your code, it is a very interesting alternative, as it works globally: procedures recognize them. Thank you!
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
Post Reply