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: =, +=, -=, *=, /=, INC and DEC. Integer types drop the decimals (toward zero) and wrap out of range; text made of digits is read, other text stops the program.
  • float is 32-bit, as in AGK: 0.1 reads back as 0.10000000149011612. Use double for clean decimals. long is 32-bit too: Visual Basic's 64-bit Long is quad.
  • x AS byte and x AS string written without a value lose their type: the variable stays untyped. Write x AS byte = 0, x AS string = "", or use int8. The same goes for int, pointer and the other names flagged in the manual.
  • int64 values only mix with other 64-bit variables: y = big + 1 into an untyped y stops 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 integer without a size compiles, but the program does not start. Write x 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))   ' 9223372036854775807
In the manual : Variables and types