Any is not a real type, but it's used as a placeholder in various ways.
Syntax
Usage
1) Dim identifier as Any ptr
or
1) Declare Sub identifier ( [ byval | byref ] identifier as Any ptr )
or
2) Dim identifier As datatype = Any
or
3) Declare Sub identifier ( byref identifier as Any )
Description
1) A special pointer type called the
Any Pointer allows pointing to any variable type. If you cast it to an
Integer Pointer, it will operate on the subject as though it were an
Integer, etc.
You may not create variables of the type
Any. It is illegal to dereference an
Any Pointer, but an
Any Pointer Pointer may be dereferenced, because doing so will result in an
Any Pointer. Trying to dereference that resulting
Any Pointer would be illegal.
This should not be confused with
Variant, a Visual Basic data type which can contain any type of variable. FreeBASIC does not natively support the
Variant type.
2) 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. It is up to the program to fill the variables with significant data. You may recognize this as the default behavior of C.
3) 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
Differences from QB
- Pointers and initializers are new to FreeBASIC.
See also