Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Initializing variables
#1
What is the difference between the two symbols "=" and "=>" for initializing variables?

Code:
dim as integer n = 2
dim as integer m => 3
? n, m

According to my tests, they seem to be equivalent.
ean Debord
----------------
Math library for FreeBasic:
http://www.unilim.fr/pages_perso/jean.de...fbmath.zip
Reply
#2
dim j as string * 10 = "hello"
url]http://fbide.sourceforge.net/[/url]
Reply
#3
The fist one is executed at run time.
With the second one you tell the compiler to intialize the variable when it makes the place for it, so at run time the data is already in.
Antoni
Reply
#4
=> was just added to resolve the ambiguities as Von showed, because FB allows constant expressions to be used when declaring fixed-len or zstring's (unlike QB). "dim s as string * 1 = "ab"" would be seen as "dim s as string * (1 = "ab")".
Reply
#5
Quote:The fist one is executed at run time.
With the second one you tell the compiler to intialize the variable when it makes the place for it, so at run time the data is already in.
Let me understand this, Antoni.

For the first one:
DIM AS INTEGER N = 2
This would be the equivalent to these 2 lines in QB: Right?
DIM N AS INTEGER
N = 2

For the second one:
DIM AS INTEGER M => 3
I cannot find an equivalent in QB. The closest would be to use a CONST, but although M is initialized to 3 when the program is loaded, M can be modified at runtime, whereas a CONST cannot. Am I right?
*****
Reply
#6
they are the same.
di i as integer {= | => } some value

it's only needed when you define fixed lenght string.
dim i as string * 10 = "hello"

this will give you an error, couse you can do arithmetic operations there. so string * 10 => "hello" is needed.

dim j as integer = 10 = 10
you'll get integer value to be -1 rather then 10.

and there was no such thing as var initalisers in qb.
url]http://fbide.sourceforge.net/[/url]
Reply
#7
Von,

You say "they are the same". Does that mean that Antoni's observations are not true?

Sorry, I get lost in your examples.
Please establish the rules first, and then follow with examples.

And for this thread, please don't mix examples with STRING. The original question of this thread dealt with DIM AS INTEGER.

Thanks.
*****
Reply
#8
i used String to clearify why it was needed.

dim i as integer = 10
dim i as integer => 10

are completely the same.
BUT why was => needed is this:

dim i as string * 10 => "some value"

compiler would see it otherwise as: * (10="some value") or as in C: * (10=="some value"). meaning it would compare 10 firts to "some value" -what cannot be done becouse of non matching types.
url]http://fbide.sourceforge.net/[/url]
Reply
#9
Thanks, Von, I think I get it now.

dim i as integer = 10
dim i as integer => 10

are completely the same.

AND, THE INTEGER I IS SET TO 10 AT RUNTIME, I ASSUME.
*****
Reply
#10
it's evulated at the compile time.
url]http://fbide.sourceforge.net/[/url]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)