Any
 
Built-in pseudo data type used in declarations and initializers

Syntax

identifier As Any Pointer|Ptr
or
Declare { Sub | Function } identifier ( [ ..., ] ByRef identifier As Any [ , ... ] )
or
Dim identifier As datatype = Any

Usage

Dim identifier as Any ptr
or
Declare Sub identifier ( byref identifier as Any )
or
Declare Sub identifier ( [ byval | byref ] identifier as Any ptr )
or
Dim identifier As datatype = Any

Description

Any can be used in three contexts: pointers, variable initializers, pointer arguments of functions and function declarations to indicate an unknown data type.

A pointer defined as an Any Ptr disables the compiler checking for the type of data it points to. It is useful as it can point to different types of data. Before dereferencing it (accessing to the data it points to) it must be Cast to a known data type.
This should not be confused with Variant, a Visual Basic data type which can contain any type of variable, which is not intrinsically supported by FreeBASIC.

Any can be used as a fake initializer to disable the default initialization to 0 of the variables. This may save time in critical sections of the programs. Is up to the program to fill the variables with significant data.

Any can be used in function parameter lists with Ptr arguments to allow the passing of any type of pointers. In this case the function must Cast the pointer argument to a known data type before accessing it.

Any can be used in function prototypes (in a Declare statement) with ByRef arguments to disable the compiler checking for the correct type of the variable passed. This use of Any is deprecated and it is only there for compatibility with QB, where it was the only way of passing arrays as arguments.

Example

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


'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



Dialect Differences

  • Not available in the -lang qb dialect.

Differences from QB

  • Pointers and initializers are new to FreeBASIC.

See also