Dynamic (Variable-length) strings ($) ($$)

Dynamic string variables (also known as variable-length) may contain an arbitrary number of characters.  Internally, each  variable uses four bytes that contain a handle number, which is used to identify and locate information about the string.  The type-specifier character is $ for an ANSI dynamic string, or $$ for a wide Unicode string.

String variables are automatically declared when the variable name is followed by one or two dollar signs ($).  You can also declare dynamic string variables using the STRING or WSTRING keywords with the DIM statement.  For example:

        DIM MyStr AS WSTRING

PowerBASIC allocates strings using the Win32 OLE string engine.  This allows you to pass strings from your program to DLLs, or API calls that support OLE strings.  The address of the contents of a non-empty string can be obtained with the STRPTR function.  The address of the string handle can be obtained with VARPTR function.  An empty (null) string may not return a valid STRPTR value.  Dynamic strings move in memory with each assignment statement: that is, STRPTR will return a different address when the content of the string is changed.  However, the associated string handle obtained by VARPTR stays constant for the duration of the life (scope) of the string variable.

LOCAL dynamic string memory and handles are released when the associated Sub, Function, Method, or Property ends.  Subsequent calls to a routine will result in new storage locations for both the handle and the string data.  The address of the handle of a STATIC or GLOBAL dynamic string stays constant for the duration of the module.  Dynamic strings and field strings cannot be part of UDT (User-Defined Type) or UNION structures.  In C/C++, a dynamic wide string ($$) is referred to as a BSTR data type.

 

See Also

Nul-Terminated Strings

FIELD strings

Fixed-length strings

String expressions