Dynamic (Variable-length) strings ($)

Dynamic string variables contain character data of arbitrary length.  Internally, each string variable uses four bytes that contain a handle number, which is used to identify and locate information about a string.  Dynamic strings can contain up to approximately 2 Gb (2^31) characters.  The type-specifier character for a dynamic string is: $.

String variables are designated by following the variable name with a dollar sign ($) or the DEFSTR type definition.  You can also declare dynamic string variables using the STRING keyword with the DIM statement.  For example:

DIM MyStr AS STRING

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.  Note, however, that Visual Basic and Visual C++ store data in OLE strings using 16-bits per character (Unicode format) while PowerBASIC stores them in 8-bit format.  In PowerBASIC, strings may contain either ASCII or ANSI string data.

The distinction between ASCII and ANSI only becomes important when using the strings for specific tasks.  For example, when dealing with API calls, string data is usually interpreted as ANSI data by the API functions, whereas PowerBASIC statements such as UCASE$ treat the string data as ASCII.

Most standard DLLs designed to work with Visual Basic should still work with PowerBASIC, because VB converts OLE strings from Unicode to ANSI before passing them to a DLL, and PowerBASIC will accept and work with the ANSI string data.

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 or Function ends.  Subsequent calls to a Sub or Function 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 string is referred to as a BSTR data type.

 

See Also

ASCIIZ strings

FIELD strings

Fixed-length strings

String expressions