Skip to content

ANY


Any 关键字在多种情况下用作类型或值的占位符。

语法

Dim identifier As Any Pointer|Ptr

Declare Sub|Function identifier ( Byref identifier As Any [ , ... ] )

Dim identifier(Any [, Any...]) As DataType

[ Declare ] { Sub | Function } proc_name ( param(Any [, Any...]) As DataType )

Dim identifier As DataType = Any

New DataType ( Any )

New(address) DataType [count] { Any }

Instr|InstrRev ( identifier, Any substring )

Procptr ( identifier, [Virtual] Any )

说明

  • 指针(第 1 种语法):
  • Byref 参数(第 2 种语法):
  • 数组维度(第 3/4 种语法):
  • 初始化(第 5/6/7 种语法):
  • Instr/InstrRev(第 8 种语法):
  • Procptr(第 9 种语法):

示例

start GeSHi

vb
Declare Sub echo(ByVal x As Any Ptr) '' echo will accept any pointer type

Dim As Integer a(0 To 9) = Any '' this variable is not initialized
Dim As Double  d(0 To 4)

Dim p As Any Ptr

Dim pa As Integer Ptr = @a(0)
Print "Not initialized ";
echo pa       '' pass to echo a pointer to integer

Dim pd As Double Ptr = @d(0)
Print "Initialized ";
echo pd       '' pass to echo a pointer to double

p = pa     '' assign to p a pointer to integer
p = pd     '' assign to p a pointer to double      

Sleep

Sub echo (ByVal x As Any Ptr)
    Dim As Integer i
    For i = 0 To 39
        'echo interprets the data in the pointer as bytes
        Print Cast(UByte Ptr, x)[i] & " ";
    Next
    Print
End Sub

end GeSHi

start GeSHi

vb
'Example of ANY disabling the variable type checking
Declare Sub echo (ByRef a As Any) '' ANY disables the checking for the type of data passed to the function

Dim x As Single
x = -15
echo x                  '' Passing a single to a function that expects an integer. The compiler does not complain!!            
Sleep

Sub echo (ByRef a As Integer)
  Print Hex(a)        
End Sub

end GeSHi

start GeSHi

vb
Dim a(Any) As Integer ' 1-dimensional dynamic array
Dim b(Any, Any) As Integer ' 2-dimensional dynamic array
Dim c(Any, Any, Any) As Integer ' 3-dimensional dynamic array
' etc.

' Further Redims or array accesses must have a matching amount of dimensions
ReDim a(0 To 1)
ReDim b(1 To 10, 2 To 5)
ReDim c(0 To 9, 0 To 5, 0 To 1)

end GeSHi

方言差异

与 QB 的差异

  • 指针和初始化器是 FreeBASIC 的新特性。

另请参阅

  • Dim
  • Declare

返回 目录

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