Statement to store data at compile time.
Syntax
Data constant_expression1 [,constant_expression2]...
Description
Data stores a list of constant numeric or alphabetical expressions that are evaluated at compile time 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 a
Data is read, the first element of the following
Data will be read. If a
Read is attempted after the last element of the last
Data, an error will occur.
Data statements are only visible from within the module in which they are defined.
Data constants can only be of simple types (numeric or string). String constants must be enclosed in double quotes.
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.
Restore label makes the first
Data item after the
label the next item to be read, thus altering the order in which the items are
Read.
Data is normally used to initialize variables. FreeBASIC also allows the initialization of static variables in their declarations; see
Variable Initializers for more information.
Example
' Create an array of 5 integers and a string to hold the data.
Dim h(4) As Integer
Dim hs As String
Dim read_data As Integer
' Set up to loop 5 times (for 5 numbers... check the data)
For read_data = 0 To 4
' Read in an integer.
Read h(read_data)
' Display it.
Print "Number"; read_data;" = "; h(read_data)
Next
' Spacer.
Print
' Read in a string.
Read hs
' Print it.
Print "String = " + hs
' Await a keypress.
Sleep
' Exit program.
End
' Block of data.
Data 3, 234, 435/4, 23+433, 87643,"Good"+ "Bye!"
Dialect Differences
- -lang fb and -lang fblite considers data items as constant expressions that are evaluated during compilation and its result stored in the program.
- -lang qb considers data items as literals and stores then without change, as in QB.
Differences from QB
- Alphabetic string literals now must be enclosed within quotation marks, in QB this was optional.
- In QB empty items evaluated to number 0 or to empty strings, in FreeBASIC they give a compile error. In QB a comma after the last item of a line of data made an empty item (between the comma and the carry return) and evaluated to 0 or empty string. In FreeBASIC that's an error.
See also