During FITC this past weekend, I saw a presenter showing code such as:
var x, y:Number;
This innocent looking line may seem like declaring two variables x & y as the Number data type. However, what it actually does is declaring variable x with an undefined data type, and y as a Number.
To test, try this:
x = "";
Test movie, all is fine. No error message.
Now add this line:
y = "";
Test movie, and you’ll see the error message:
‘Type mismatch in assignment statement: found String where Number is required. y = “”;’
So remember to declare variables as intended, such as:
var x:Number;
var y:Number;
4 replies on “Declaring variable type”
or
var x:Number, y:Number;
Hi Dave,
Thats great! Nice of you to hide the identity of the presenter. What I want to know is if there is a workaround for this. Declaring similar variables on the same line can have its benefits (readability or simply cuz teh coder does not want to add comments for similar types of variables and wants to bunch them all together cuz they all make sense as a group). Any way this can be acheived?
Hey Paul, Tink’s comment shows it all on one line. Personally, I prefer to put each variable on a separate line for most vars. However, I understand the desire to put x & y on one line because they are related (if we’re talking about coordinates).
Putting vars in one line will help cleaning messy code. It’s good stuff.
Best regards
Chris
zakupy przez internet