Syntax
Gives a variable or an array a type. From then on, every value stored in it is converted to that type.
[GLOBAL | LOCAL] <name> AS <type> [= <value>]
[GLOBAL | LOCAL] <name> AS <type>[] [= [<value>, …]]
[GLOBAL | LOCAL] <name> AS <type>[<size>[, <size>…]] [= [<value>, …]]
DIM <name>(<size>) AS <type>
<name>.<suffix> = <value>Also written : integer, string, byte, int64
Parts
| <name> | The variable's name, with no sigil: name$ AS string compiles, but the program does not start. |
|---|---|
| <type> | A type name, case-insensitive: int8 (byte), uint8 (ubyte), int16 (word), uint16, int32 (integer, long), uint32, int64 (quad), uint64, float32 (float), float64 (double), string, handle, taskhandle, or a TYPE name. |
| <value> | Optional starting value, converted to the type. Without it, a number starts at 0, a string at "", a handle at NULL. |
| <size> | The last valid index of one dimension. AS <type>[] with no size starts an empty array (.length is -1). |
| <suffix> | A PureBasic one-letter type: b a w u c l i q f d s. The first name.f = 1.5 declares the variable as if with AS. |
Good to know
- The type acts on each store:
=,+=,-=,*=,/=,INCandDEC. Integer types drop the decimals (toward zero) and wrap out of range; text made of digits is read, other text stops the program. floatis 32-bit, as in AGK:0.1reads back as0.10000000149011612. Usedoublefor clean decimals.longis 32-bit too: Visual Basic's 64-bitLongisquad.x AS byteandx AS stringwritten without a value lose their type: the variable stays untyped. Writex AS byte = 0,x AS string = "", or useint8. The same goes forint,pointerand the other names flagged in the manual.int64values only mix with other 64-bit variables:y = big + 1into an untypedystops the program. A literal above 9007199254740991 must be written as text:"9223372036854775807".- A typed FOR counter does not wrap during the loop.
DIM x AS integerwithout a size compiles, but the program does not start. Writex AS integer.
Example
lives AS integer = 7.9hits AS uint8 = 250hits = hits + 10title AS string = "Hero"price AS float = 0.1exact AS double = 0.1big AS int64 = "9223372036854775807"Console(Str(lives) + " " + Str(hits) + " " + title) ' 7 4 HeroConsole(Str(price) + " " + Str(exact)) ' 0.10000000149011612 0.1Console(Str(big)) ' 9223372036854775807In the manual : Variables and types