ANY
- 来源: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgAny
- 最后更新: 2025-07-24
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 Subend 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 Subend 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
方言差异
- 在 -lang qb 方言中不可用。
与 QB 的差异
- 指针和初始化器是 FreeBASIC 的新特性。
另请参阅
DimDeclare
返回 目录