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.

Newcomers DBPro Corner / Use of #contant

Author
Message
BandP College Man
16
Years of Service
User Offline
Joined: 3rd Jul 2008
Location: Dorset (U.K.)
Posted: 31st May 2009 18:10
I was working on some code and, according to my training, trying to avoid using 'magic numbers' by utilizing constants. I discovered that when setting a constant to a calculation (e.g. #constant Area = max*min), that this would work if 'max' and 'min' were variables but not if 'max' and 'min' were constants themselves.

Is this a bug or am I missing something?
Rudolpho
18
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 31st May 2009 19:12
I believe it reads the constants from the top of your source file(s) and down, so declaring the min and max constants before Area should probably work.
As far as I know, you doesn't need to use the "=" operand when declaring constants; anything to the right of #constant, trimmed spaces, will replace the constant name. What I mean with this is that if the equality sign is not removed (which it may be, I don't know), it would replace all instances of the constant with "= [value]", which would probably work properly / even compile. Just a suggestion.

bobbel
15
Years of Service
User Offline
Joined: 5th Jan 2009
Location: In my DBPro case xD
Posted: 31st May 2009 20:12
this program doesn't work



it prints zero instead of 15. when i remove the first 2 constants and make them just regular variables, it does work. i think it has something to do with the way constants work. the compiler just removes every constant with the thing whats saved in his memory. so if you have

#constant a "Hi"
print a

the compiler just compiles this

#constant a "Hi"
print "Hi"

and because of that, dark basic pro wil always think that the things behind the constant name are variables. so if you look at this

#constant c a+b

dbp will just see them as variable a and b and not constant a and b. thats why c = 0. but thats just my thought, i could be horribly wrong

*ding ding*
BMacZero
18
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 1st Jun 2009 01:07
I think that's it, bobbel. Constants are NOT variables and are not related to variables. They are replaced before the program is compiled, like Rudolpho said.



Diggsey: I have a spine and memory, but one memorable guy says he hates me. What am I?

Login to post a reply

Server time is: 2024-11-25 06:28:36
Your offset time is: 2024-11-25 06:28:36