DATA
- 来源: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgData
- 最后更新: 2016-02-10
在编译时存储数据的语句。
语法
` Data constant_expression1 [,constant_expression2]...
`
描述
Data 存储常量数值或字符表达式列表,在编译时求值(使用 -lang qb 时除外),以常量形式存储,可通过 Read 语句读入变量。
程序中所有 Data 语句作为一个链式列表;读取完一个 Data 语句的最后一个元素后,将读取下一个 Data 语句的第一个元素。
程序不应在最后一个 Data 元素之后尝试 Read。其结果(在所有方言中)均未定义,程序可能崩溃(页面错误)。
Data 语句只在其所在模块中可见;只能在模块级代码中使用。
Data 常量只能是简单类型(数值或字符串)。数值可作为数值字面量读入字符串。读入数值变量的字符串将通过 Val 函数求值。可以使用 Const 作为数据项,但在 -lang qb 方言中,常量名被视为普通文本。
"Restore label" 语句使 label 标签之后的第一个 Data 项成为下一个被读取的项,允许用户选择特定的数据段来读取。
Data 通常用于初始化变量。FreeBASIC 还允许在 Dim 声明静态变量时初始化,详见变量初始化器。
示例
start GeSHi
vb
' 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.
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!"end GeSHi
方言差异
- -lang fb 和 -lang fblite 将数据项视为常量表达式,在编译期间求值并将结果存储在程序中。
- -lang qb 将不带引号的单词(包括变量名和常量名)视为字符串字面量,不加修改地存储,与 QBASIC 一样。不带引号的字符串以逗号为分隔符,冒号或换行符表示
Data语句结束。不带引号的字符串会去除首尾空白。
与 QB 的区别
- 在 -lang qb 方言之外,字母字符串字面量必须用引号括起来,在 QBASIC 中这是可选的。
- 在 QBASIC 中,空项求值为数值
0或空字符串;在 FreeBASIC 中则会产生编译错误。在 QBASIC 中,语句末尾的逗号会产生额外的空项(求值为0或空字符串);在 FreeBASIC 中则会产生编译错误。
另请参阅
ReadRestore
返回 目录