Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Bug Reports / calculated Constants don't work inside functions

Author
Message
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 5th Jul 2012 00:49 Edited at: 5th Jul 2012 00:54
Originally, "t" calculated some value, but even simplified this way the value of THING still returns 0 when called from a function.

I can get around the bug by using Globals instead.




Or, to absolutely use constants, I can do this:


"You're not going crazy. You're going sane in a crazy world!" ~Tick
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 6th Jul 2012 22:12 Edited at: 6th Jul 2012 22:13
That's not a bug. It's the way constants are designed to work.

The following code



is equivalent to



which makes it clear why your program behaves the way it does.

In other words: define t as a global and your problem disappears.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 7th Jul 2012 06:11
I thought constants were suppose to have a global scope?

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Kevin Picone
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 7th Jul 2012 08:20
Nah, the codes mixing pre-evaluation with runtime evaluation. So the assignment (t = 48) occurs at runtime, and the constant stuff at compile time. So the pre-processor sees the constant(Macro declaration) and just drops the T text in it's place throughout the source.

Then on the second pass, the compiler will see T is just regular variable, so any assignment in the main scope isn't going to be visible in functions. Without making T global

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 7th Jul 2012 14:13
Yes, a "constant" in the DBPro sense is simply a text replacement before the compiler starts examining the syntax etc. It's often used as a way of identifying fixed constants to make the code more readable, so instead of littering your code with the meaningless number 23, say, you might put it in a constant



That's probably the most common use of DBPro constants but you can define a constant to be anything. For example you might do something like this when debugging code:



Admittedly in that example a function would probably be simpler but there are cases where you can get "constants" to do useful things.

Login to post a reply

Server time is: 2024-04-20 04:09:09
Your offset time is: 2024-04-20 04:09:09