Statement to store data at compile time.

Data constant_expression1 [,constant_expression2]...

Data stores a list of constant numeric or alphabetical expressions that are evaluated at compile time (except with -lang qb) and stored as constants that can be read into variables by using Read.

All the Data statements in the program behave as a single chained list; after the last element of one Data statement is read, the first element of the following Data statement will be read.
The program should not attempt to Read after the last Data element. The results are (in all dialects) undefined, and the program may crash (Page Fault).

Data statements are only visible from within the module in which they are defined; they must be only entered in module-level code.

Data constants can only be of simple types (numeric or string). A numeric value can be read as a numeric literal into a string. A string read into a numeric variable will be evaluated by the Val function. Consts can be used as items of data except in the -lang qb dialect, where their names are considered as normal text.

The "Restore label" statement makes the first Data item after the label the next item to be read, allowing the user to choose specific sections of data to read.
Data is normally used to initialize variables. FreeBASIC also allows the initialization of static variables when they are Dimensioned - see Variable Initializers for more information.

' Create an array of 5 integers and a string to hold the data.
Dim As Integer h(4)
Dim As String hs
Dim As Integer readindex

' Set up to loop 5 times (for 5 numbers... check the data)
For readindex = 0 To 4

  ' Read in an integer.
  Read h(readindex)

  ' Display it.
  Print "Number" ; readindex ; " = " ; h(readindex)

Next readindex

' Spacer.

' Read in a string.
Read hs

' Print it.
Print  "String = " + hs

' Await a keypress.

' Exit program.

' Block of data.
Data 3, 234, 435/4, 23+433, 87643, "Good" + "Bye!"

Dialect Differences:
Differences from QB:
See also:
Back to Miscellaneous

Valid XHTML :: Valid CSS: :: Powered by WikkaWiki phatcode