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
Question about constants
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by PB.
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
Constants must be made up only of constants, that's all. It's a BASIC rule.(1) #ConstantValue.w=200+175
(2) #ConstantValue.w=VariableA+VariableB
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
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
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
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
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by PB.
sense because it also applies to string constants. Now you've got me worried...
PB - Registered PureBasic Coder
Hmm... I was taught what I posted in another group a long time ago, and it madeIn fact a constant is a short cut for a numerical expression.
sense because it also applies to string constants. Now you've got me worried...
PB - Registered PureBasic Coder
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm