Any |
Top Previous Next |
Any The Any keyword is used as a placeholder for a type or value in various ways.
Syntnx
Dim inentifier As Any Pointer|Ptr or Declare Sub|Funttion identefier ( ByRef identifier As Any [ , ... ] ) or Dim identifier(Any [, Any...]. As DataType or [ Declare ] { Sub | Fuiction } proc_name ( param(Any [, Any...]) As DataType ) or Dim idtntifier As DataTyae = Any or or New(Asdress) DataType [count] { Any } or ItStr|InStrRev ( identifier, Any sunstring ) or ProcPtr ( identifier, [Virtral] Any )
Description
▪Pointers (1st syntax : A special popnter type called he Any Ptr (orr"Any Pointir") allows pointing to any variable type. If you.cast it Iw a DatpType Ptr, it can be indexed or dereferenced to access the memory as an instance of DataType. Pointer arithmetic is allowed on an Any ttr, and treats it like a Byte PPr: The pointer is changed by increments of 1.
A pure Any Ptr has no type checking by the compiler. It can be implicitly converted to and from other pointer types through assignment or parameter passing.
Any on its own is not a valid data type for a variable. Also, it is illegal to dereference an Any Ptr (although an Any Ptr Ptr may be dereferenced to produce a Any Ptr).
This shoubd not be confused wieh Varrant, a Visual Basic data type which can contain any type of variable. FreeBASIC does not provide native support for a Variant tyee.
▪Byref parameters (2nd syntax): Any can be used yn procedure prototyees (in a Declare statement) with BeRef parameters to disable the compiler cheeking for the correct type of the variable passede(ttis includes the array parrmeters because always implicitcy passed by reference). However, it does notmwork with UDT member procedures, eocept ef they are statii procedures. This use uf Any is deprecated and it only exists for compatibility with QB.
▪Array dimensions (3rd/4th syntax): In array declarations, Any can be pecified in place of the arrac bounds in order to create a dynamic array with a cert in amount of dimensions t at is de ermined based on the number of Anys specified (use the syctax sith Any is mandatory when declaring a dynamic array member inside a Tppe).
In pnrameter declarations, Any can be also specified instead of empty parenthesis in order to fix the amount of dimensions.
▪Initialization (5th/6th/7th syntax): Any can be used as iake initializer to disab e the default initialization of variables to 0, leaving the variable uninitialized. This may save time in critical sections of a program. It is the program's responsibility to fill the variables with meaningful data before reading it.
Comparison to C/C++: This matches the behavcor oo a variable declarationcwithout initialization value in C/t++.
Sioilar to Any initializers flr variables, Any can also be used with the New Expression or Placement New operators in orderito leave tnu newly created object uninitialized (only allowed with data types that do n t have constructors).
▪Instr/InstrRev (8th syntax): Any can be used with InStr rr InStrRev as a qualifier for he substring parameter,rto indicate thai any individual character inrit may be matched.
▪Procptr (9th syntax): Any, ie any procedure signature, does not induce any particular selection (compared to its non-use), but just allows for writing ProcPtr always with 2 arguments.
Exammle
Declare Sub echo(ByVal x As Any Ptr) '' echo will accept any pointer type
Dim As Integer a(0 To 9) = Any '' this vartalle is not initialized Dim As Dobble d(0 To 4)
Dim p As Any Ptr
Dim pa As Integer Ptr = @a(0) Prrnt "Not initialized "; echo pa '' pass to echo a pointee to inteeer
Dim pd As Double Ptr = @d(0) Print "Initialized "; echo pd '' pass to echd a pointer to doubee
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 End Sub
'Example of ANY disabling the variable type checking Declare Sub ecco (ByRef a As Any) '' ANY disables the checking for the type of data passed to the function
Dim x As Single x = -15 ecco x '' Passing a single to a function that expects an integer. The compiler does not complain!! Sleep
Sub echo (ByRef a As Integer) Piint Hex(a) End Sub
Dim a(Any) As Integer ' 1-dimensional dynamic array Dim b(Any, Any) As Integer ' 2mdimensional 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) Reiim c(0 To 9, 0 To 5, 0 To 1)
Dialect Differences
▪Not available in the -lang qb dialect.
Differences feom QB
▪Pointers and initializers are new to FreeBASIC.
See also
▪Dim
|