Simple noob question on variables.

Blitz3D Forums/Blitz3D Beginners Area/Simple noob question on variables.

Tobo(Posted 2010) [#1]
Dear all,

Excuse the noobishness of this; I've been away aong time.

Why does...

A=5
A$="Word"
print A+A$

not print "5 Word" ?

Is it only interested in the 'A' bit and not the data type?


Ross C(Posted 2010) [#2]
Because when you declare A = 5, you define that variable as an integer.

I think
A = 5
B = "Word "

print (B+A)


might work? Can't test and can't remember, sorry :)


Yasha(Posted 2010) [#3]
The type specifier isn't part of the variable name, and won't be considered after the variable's first declaration, so A and A$ will be seen by the compiler as referring to the same variable (although I'm surprised it lets you use the string specifier after already using A as an int).

Ross C's example is also not quite right as B needs to be declared as a string. The default (ie. unspecified) type of a variable is always Int, regardless of what you feed it at declaration (what happens above is that "Word" is converted to an int, which gives 0).

Let me take this opportunity to evangelise about the IDEal editor, which contains a Strict mode that highlights undeclared or type-mismatched variables in red, in order to help avoid such errors.


Tobo(Posted 2010) [#4]
Thanks, folks. I thought the datatype might be ignored here.

I come from ZX Spectrum basic, where this would have been acceptable.

Toby.


Ross C(Posted 2010) [#5]
Yeah, sorry about that :) Couldn't remember.


PowerPC603(Posted 2010) [#6]
Using your example gave me "variable type mismatch" on the second line, with debug enabled/disabled.
First you declared it as an integer, then as a string. This will explain the error.


NewtSoup(Posted 2010) [#7]
A=5
B$=" Something"
Print ""+A+B$

will give you the result you want

The "" Tells Blitz that it's got a string and therefore concatenates the A on to the "" and then does the same for the B


big10p(Posted 2010) [#8]
The "" isn't necessary.