Dim
 
声明一个变量

语法

Dim [Shared] name1 As DataType [, name2 As DataType, ...]
or
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可用于声明和赋值任何受支持的数据类型,用户定义类型或枚举的变量。

根据声明变量或数组的位置和方式,可以更改内存中的赋值方式。见Storage Classes .

通过用逗号分隔每个变量声明,可以在单个Dim语句中声明多个变量。

'' 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

'' Compile with -lang qb

'$lang: "qb"

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

'' I and J are INTEGERs
'' X and Y are SINGLEs
'' T$ is STRING
'' D is DOUBLE

Dim I, J, X, Y, T$, D As Double


数组

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

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。

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

'' The first dimension of the declared array 
'' has indices from 1 to 2, the second, 0 to 6, 
'' and the third, 3 to 5.



有关数组的更多信息,请参阅数组概述.

如果与Dim声明数组的维数的值都是常量,则将创建数组Static(除非指定Option Dynamic),同时使用一个或多个变量来声明数组的维度即使Option Static有效,它的长度也是可变的。

数组可以以多种方式声明为可变长度:使用Dim与一组空的索引集(Dim x()),使用Dim索引为变量或使用关键字ReDim或使用Any阵列的地方界限,或声明它通过元命令$Dynamic.可变长度的数组不能使用初始化器。

使用Dim声明且不超过Option Dynamic的数组的数组是固定长度(在运行时不可调整),可以使用初始化器。

上限可以是省略号(...,3点)。这将导致根据初始化程序中找到的元素数量自动设置上限。当以这种方式使用省略号时,必须使用初始化器,并且它可能不是Any.有关一个简短的例子,请参阅省略页面。

另见Fixed-Length Arrays Variable-Length Arrays .

初始化器

数组,变量,字符串和用户定义的类型(UDT)在创建时被默认初始化为零或为空字符串。

为了避免默认变量初始化的开销,Any初始化程序可以与Dim一起使用,以使编译器只在存储器中保留变量的位置,但不对其进行初始化,因此该变量将包含垃圾。在这种情况下,程序员不应该对初始值做出假设。

固定长度数组,变量,zstrings和UDT可以通过使用初始化程序跟随变量声明在其声明时给出一个值。注意初始化不同类型之间的区别。数组,变量和UDT将按照正常赋值进行初始化,使用等号(=)符号。可以使用=>符号,允许在声明固定长度字符串时避免声明类似于表达式。

数组值以逗号分隔的值由大括号括起来,UDT值以括号括起来的逗号分隔值给出。这些初始化变量的方法可以嵌套在一起,用于复杂的赋值。嵌套允许初始化任何维度的数组。

'' 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)}


对于模块级,固定长度或全局变量,初始化值必须是常量表达式。否则FreeBASIC将报告编译时错误。

注意:此时不支持使用字符串初始化UDT。初始化包含数据字段初始化程序或字符串的UDT无效。

具有类型后缀的显式变量

-lang qb -lang fblite 方言中,变量的数据类型可以用后缀(%%#!&)表示)。

'' Compile with -lang qb or fblite

'$lang: "qb"

'' A string variable using the $ type suffix
Dim strVariable$

'' An integer variable using the % type suffix
Dim intVariable%

'' A long variable using the & type suffix
Dim lngVariable&

'' A single precision floating point variable using the ! type suffix
Dim sngVariable!

'' A double precision floating point variable using the # type suffix
Dim dblVariable#


例子

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   ''固定长度字符串
Dim s2 As String        ''可变长度字符串
Dim s3 As ZString Ptr   ''zstring

s1 = "你好,世界!"
s2 = "来自FreeBASIC的Hello World"
s3 = Allocate( Len( s2 ) + 1 )
*s3 = s2

Print "字节:"; Len(a)
Print "短:"; Len(b)
Print "整数:"; Len(c)
Print "Longint型:"; Len(d)
Print "UByte:"; Len(au)
Print "UShort:"; Len(bu)
Print "UInteger:"; Len(cu)
Print "ULongint:"; Len(du)
Print "单:"; Len(e)
Print "双:"; Len(f)
Print "整数指针:"; Len(g)
Print "字节指针:"; Len(h)
Print "固定字符串:"; Len(s1)
Print "变量字符串:"; Len(s2)
Print "ZString:"; Len(*s3)

Deallocate(s3)


方言差异

与QB差别

  • 可变初始化器是FreeBASIC的新功能。
  • 替代语法Dim As DataType symbolname ,[...]是FreeBASIC的新功能。
  • 多维数组以这个确定的顺序存储:仅在最后一个索引中不同的值是连续的(行主序),默认情况下它们在QB中以相反的顺序存储:仅在第一个索引中不同的值是连续的(列主要顺序)。
  • 在FreeBASIC中,可以有大小为2 GiB的可变长度数组。在QB中,如果$ DYNAMIC /AH 被使用,$ STATIC 数组被限制为64 KiB,或者可用的DOS内存(最多为几百个KiB)。
  • 上限的省略号形式是FreeBASIC的新功能。

参考