Question about constants

Everything else that doesn't fall into one of the other PB categories.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.

Hi all,
a constant is an integer value (in the future also a float and a string ) witch is set once and you are not able to change it anymore, right?

Well I can code this:

#ConstantValue.w=200+175 ;two different values combined

Now if I want to make this:

VariableA.w=200
VariableB.w=175
#ConstantValue.w=VariableA+VariableB

I get an error message:
"A constant can't be composed by a variable or a function"
Why?

Once the constant is set the variables can change but the constant has to remain the same, because you can't alter it.

Example:
NewPassWord.l=InputBox("Please enter your password","Only numbers for now...",0)
RequestedPassWord.l=InputBox("Please confirm your password","The same number please...",0)
If NewPassWord=RequestedPassWord
#PassWord.l=NewPassWord ;want be sure that nobody and nothing can alter this value!
;Now store #PassWord in a specified place... or whatever
EndIf
(You are right, this was a bad example...)

Am I wrong? Or is it too difficult to achieve? To human?



Have a nice day...
Franco
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.
(1) #ConstantValue.w=200+175
(2) #ConstantValue.w=VariableA+VariableB
Constants must be made up only of constants, that's all. It's a BASIC rule.

Example (1) works because the numbers 200 and 175 are constants (ie. the number
200 or 175 isn't going to "change" to something else, ever).

Example (2) doesn't work because VariableA and VariableB are not constants,
and their values are subject to change at any time. It doesn't matter that
they're "set" to a value when the constant is being declared with them; they
are still variables and not constants.

As for your wish of storing a user-specified password in a constant so that
the password cannot change, ever; well just store it in a variable when the
user specifies it and ensure that your app doesn't change it anywhere else
in your code. It'll thus stay the same, effectively acting like a constant.


PB - Registered PureBasic Coder


Edited by - PB on 18 January 2002 00:40:53
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

In fact a constant is a short cut for a numerical expression. When the compiler see a constant, it just take its value and put it in the code. You now see why your example can't work... You can alter a constant by assigning another numerical values.

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.
In fact a constant is a short cut for a numerical expression.
Hmm... I was taught what I posted in another group a long time ago, and it made
sense because it also applies to string constants. Now you've got me worried...


PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Of course, string constants are correct too.

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.

Thanks for lighten me up
So the coder has to make sure that, how big the code might be, never, never, never touch a variable with a constant value.
I appreciate your comments.


Have a nice day...
Franco
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

You can neame you're constant variable differently to be sure and set it only at one place in your code:

CONSTANT_Password = "Hehe"

Fred - AlphaSND
Post Reply