Skip to content

DIM

来源: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgDim 最后更新: 2024-03-07

声明变量名称并为其分配内存。

语法

声明变量:

Dim [Shared] name1 As DataType [, name2 As DataType, ...]
Dim [Shared] As DataType name1 [, name2, ...]

数组:

Dim name ( [lbound To] ubound [, ...] ) As DataType
Dim name ( Any [, Any...] ) As DataType
Dim name ( ) As DataType

初始化器:

Dim scalar_symbol As DataType = expression | Any
Dim array_symbol (arraybounds) As DataType = { expression [, ...] } | Any
Dim udt_symbol As DataType = ( expression [, ...] ) | Any

描述

声明变量名称并为其分配内存。在 -lang fb 方言中,或在其他方言中使用 Option Explicit 时,变量在使用前必须先声明。只有在 -lang qb-lang fblite 方言中,变量才可以不先声明就使用,这类变量称为隐式变量。

Dim 可用于声明和赋值任何支持的数据类型、用户定义类型或枚举的变量。变量或数组的声明位置和方式会影响其在内存中的分配方式,详见 存储类。在单个 Dim 语句中,可以用逗号分隔声明多个变量。

vb
'' Variable declaration examples
'' One variable per DIM statement
Dim text As String
Dim x As Double

'' More than one variable declared, different data types
Dim k As Single, factor As Double, s As String

'' More than one variable declared, all same data types
Dim As Integer mx, my, mz ,mb

'' Variable having an initializer
Dim px As Double Ptr = @x

带隐式数据类型的显式变量

-lang qb-lang fblite 方言中,即使变量是显式声明的,如果既未给出数据类型名,也未给出类型后缀,则会被赋予默认数据类型。默认数据类型在 -lang qb 方言中为 Single,在 -lang fblite 方言中为 Integer。默认数据类型可以在整个源代码中通过 Def### 语句更改(例如 DefIntDefStrDefSng)。

vb
'' Compile with -lang qb
'$lang: "qb"

'' All variables beginning with A through N will default to the INTEGER data type
'' All other variables default to the SINGLE data type
DefInt I-N

Dim I, J, X, Y, T$, D As Double
'' I and J are INTEGERs
'' X and Y are SINGLEs
'' T$ is STRING
'' D is DOUBLE

数组

与大多数 BASIC 方言一样,FreeBASIC 支持索引范围从下界到上界的数组。在所示语法中,lbound 指下界(最小索引),ubound 指上界(最大索引)。如果未指定下界,则默认为零,除非使用了 Option Base

vb
Const upperbound = 10
'' Declare an array with indexes ranging from 0 to upperbound,
'' for a total of (upperbound + 1) indexes.
Dim array(upperbound) As Single

多维数组同样可以声明,存储顺序为:仅最后一个索引不同的值相邻(行主序)。多维数组的最大维数为 8。

vb
'' declare a three-dimensional array of single
'' precision floating-point numbers.
Dim array(1 To 2, 6, 3 To 5) As Single

初始化器

数组、变量、字符串和用户定义类型(UDT)在创建时默认初始化为零(BooleanFalse)或空字符串。要避免默认变量初始化的开销,可以将 Any 初始化器与 Dim 一起使用,告诉编译器仅在内存中保留变量的位置而不初始化它,因此变量将包含垃圾值。

vb
'' Declare an array of 2 by 5 elements and initialize
Dim array(1 To 2, 1 To 5) As Integer => {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}}

'' declare a simple UDT
Type mytype
    var1 As Double
    var2 As Integer
End Type

'' declare a 3 element array and initialize the first 2 mytype elements
Dim myvar(0 To 2) As mytype => {(1.0, 1), (2.0, 2)}

示例

vb
Dim a As Byte
Dim b As Short
Dim c As Integer
Dim d As LongInt
Dim au As UByte
Dim bu As UShort
Dim cu As UInteger
Dim du As ULongInt
Dim e As Single
Dim f As Double
Dim g As Integer Ptr
Dim h As Byte Ptr
Dim s1 As String * 10   '' fixed length string
Dim s2 As String        '' variable length string
Dim s3 As ZString Ptr   '' zstring

s1 = "Hello World!"
s2 = "Hello World from FreeBASIC!"
s3 = Allocate( Len( s2 ) + 1 )
*s3 = s2

Print "Byte: "; Len(a)
Print "Short: "; Len(b)
Print "Integer: "; Len(c)
Print "Longint: "; Len(d)
Print "UByte: "; Len(au)
Print "UShort: "; Len(bu)
Print "UInteger: "; Len(cu)
Print "ULongint: "; Len(du)
Print "Single: "; Len(e)
Print "Double: "; Len(f)
Print "Integer Pointer: "; Len(g)
Print "Byte Pointer: "; Len(h)
Print "Fixed String: "; Len(s1)
Print "Variable String: "; Len(s2)
Print "ZString: "; Len(*s3)

Deallocate(s3)

版本

  • 自 fbc 1.20.0 起,STRING*N 类型的固定长度字符串初始化为空格值(之前为零值)。

方言差异

  • -lang qb-lang fblite 方言中,若变量定义在过程内,则具有过程作用域;若用 Dim Shared 定义,则对整个模块可见。
  • -lang qb 方言中,变量不能初始化。在 -lang fblite 方言中,变量在过程/模块开始时以默认值初始化,并在运行时执行 Dim 语句时赋予初始值。
  • -lang fb-lang deprecated 方言中,在复合块语句内声明的变量具有局部作用域,仅在这些块内可见。

与 QB 的区别

  • 变量初始化器是 FreeBASIC 新增的。
  • 替代语法 Dim As DataType symbolname, [...] 是 FreeBASIC 新增的。
  • 多维数组以行主序存储(QB 默认使用列主序)。
  • FreeBASIC 中可变长度数组最大可达 2 GiB。
  • 上界的省略号形式是 FreeBASIC 新增的。

另请参阅

基于 FreeBASIC 官方文档翻译 如有侵权请联系我们删除
FreeBASIC 是开源项目,与微软公司无隶属关系