Variables

Variables may have any name as long as it starts with letter. Variable names are not case sensitive.

When a program starts there are no variables DIMmed. If you put the DEFLONG command at the top of a source file it will pre-DIM variables A to Z as LONG. Handy of course, but this adds significant overhead to internal stuff to do with variables as slows things down a bit. Also causes you to write shoddy code that is difficult to maintain.

Bas9k will generally try to do type casting where possible/sensible. Here is a summary of what works/don’t work:

Works:

LONG -> DOUBLE
DOUBLE -> LONG
LONG -> STRING
DOUBLE -> STRING

Don’t:

STRING -> LONG
STRING -> DOUBLE

Pretty obvious really. These conversions work in expressions so you have a lot of flexibility, but remember this:

Expressions are largely parsed left to right. Each part of an expression is processed according to an order of precedence, and temporary values are stored in temporary variables. They are given a ‘best guess’ data type. If an expression develops that causes an invalid type conversion you will get an error, without actually having declared a variable of the wrong type.

Also, numeric literals are assigned to temporary LONG variables unless they are non-integer. If that value should appear on the left of an expression the result will be a LONG (and therefore an integer) even if the right hand side was a DOUBLE variable. Force a DOUBLE calculation by specifying say 6.0 instead of 6.

Variable declarations do not need to be at the top of the source - they can be anywhere you like, and have global scope.

String literals must usually be surrounded by double quotes. The only exception to this is when used by DOS commands in the console, and the literal does not contain spaces or back-slashes.

String literals and variables may be concatenated with the ‘&’ operator, eg:

StrVal = ”Date: “ & DATE()

Numbers (numeric literals) can be specified in 2 forms: decimal or a ASCII character value (the character surrounded by single quote as in C).

The number Fifty Five can be represented as

55 or ‘7’

Obviously the second form can only be used for numbers in the range 0-255.

Siehe auch: Data Types, DIM

Startseite