Data Types
In general, there are only two types of data an application might use - strings and numbers. Unfortunately, computer limitations on how data can stored and application-specific data requirements have resulted in several variations of each type.

PowerBASIC supports 4 types of strings and 11 types of numeric data, differentiated by how the data is formatted and how many bytes of memory are required to store the data. The table at the end of this page gives specific information for each of the various data types.

In addition, PowerBASIC supports 7 other numeric data types whose content is numeric but which have usage limitations that prevent them from being treated a simple numbers in conventional numeric expressions.

Finally, PowerBASIC support 1 data type (Bit) which may only be used within a UDT structure. Both signed and unsigned versions of the data type are allowed.

Here's a categorized list of the PowerBASIC data types.

Storage Requirements
The number of bytes used to store numeric variables comes up frequently in programming. Here is the number of storage bytes for several of the numeric data types.

     1  byte
     2  integer, word
     4  long, dword, single, pointer
     8  double, currency

Storage space for all data types is given in the table at the end of this page.

Frequently-Used Data Types
Of the data types listed above, several are used far more frequently than the other types. The commonly used data types are:

  • integer
  • long integer
  • single-precision
  • double-precision
     
  • variable-length string
  • fixed-length string
  • double-word
  • variant

Other Data Types
These other data types are used less frequently, but are nonetheless very useful in specific types of applications.

  • bit
  • byte
  • word
  • extended-precision
  • quad-integer
  • currency
  • extended-currency
     
  • pointer
  • FIELD string
  • ASCIIZ string
  • GUID
  • IAUTOMATION
  • IDISPATCH
  • IUNKNOWN

User Defined Types (UDT)
Sometimes a programmer needs to work with groups of numeric and string values. This can be done by creating a string/numeric variable for each value. As a simpler way of working with multiple data values, PowerBASIC provides a special data structure called a User-Defined Type (UDT).

Here's an example of a simple UDT and how the elements of a UDT can be accessed in code.

    Type MyEmployeeData 
         name as String * 30
         division as String * 30
         employeenumber as Long
         yearsofservice as Integer
    End Type

    Dim Person as MyEmployeeData
    Person.name = "David"
    Person.division = "Customer Service"
    Person.employeenumber = 12345
    Person.yearsofservice = 15

The example above shows the two steps in creating a UDT variable. First, a named Type/End Type block of code is used to create a UDT data type. Then, a DIM statement is used to create a variable of that type.

A UDT can have elements of any PowerBASIC data types except dynamic strings and field strings. String elements in UDTs must be of fixed length (hence the restriction on dynamic/field strings). UDT arrays are supported and UDT variables may be used as arguments to procedures.

The enormous advantage of UDTs is in how easily a programmer can manipulate several pieces of data at one time. In the next example, two UDT variables are created. By setting one variable equal to the other, the entire set of element values is transferred from one variable to another.

The Put statement example below also shows how easily and entire set of UDT elements can be written to a file, rather than having to deal with each element individually.

    Dim PersonA, PersonB as MyEmployeeData
    PersonA = PersonB
    Put #1, PersonA

Other constraints of UDTs are that UDT arrays cannot be redimensioned during run-time, nor can ARRAY statements be used directly on UDT element arrays.

Union Types
Union data types are created just like UDTs. The major difference is that each element within a Union occupies the same memory location as all the others. This allows easy conversion of data from one format to another, simply by writing the data into the Union as one data format, and reading the data back as another.

Unions are infrequently used, but when needed can be very useful. See PowerBASIC Help for more details.

Pointers
A pointer is simply a variable that holds a 32-bit (4 byte) address, typically an address where the data associated with a variable is found. For example, if an integer variable i% = 4, the value 4 is location in memory somewhere. A pointer to i% would be the address in memory where the value 4 is located.

Pointers are popular with programmers because they provide significantly greater speed in performing memory operations (getting/setting variable values).

To define a pointer, a DIM statement is used. For performance reasons, PowerBASIC requires that the data type of the target variable be specified (not all languages have this requirement).

Once a variable and pointer are defined, the PowerBASIC VARPTR function can be used to assign the address to the pointer, as shown in this example.

    DIM x as Integer
    DIM xp as Integer Pointer  

    x = 2                'x = 2
    xp = varptr(x)       'xp is assigned memory location of x

This basic code for assigning the value of a pointer works with all variables, except for dynamic strings. PowerBASIC provides a special function STRPTR for working with dynamic string pointers.

Dynamic strings themselves use handles, which means that using VARPTR on a string will return the handle to a string VARPTR(a$). The STRPTR function, however, will actually point to the string data itself.

    Dim s as string, sp as String Pointer
    s = "hello"
    sp = VARPTR(s)     ' sp assigned the handle of s
    sp = STRPTR(s)     ' sp assigned the address of the string data

STRPTR cannot be used with fixed-length or ASCIIZ string because they do not use string handles. STRPTR can be used with dynamic and field strings.

By using the @ as a prefix to a pointer, such as @sp from the example above, a program can directly access the value, just as though the @sp were the variable name itself. Here are some examples.

    Dim x As Integer, y As Integer, z as Integer
    Dim xp As Integer Pointer, yp As Integer Pointer

    x = 43
    y = 12

    xp = VarPtr(x)
    yp = VarPtr(y)

    z = @xp           ' z = 43   @xp access the value of x, not it's pointer address
    z = @xp+y         ' z = 55   @xp is used as though it were a variable
    @yp = @xp         ' y = 43   the value of y is assigned the value of x

Using a pointer with a value of zero will cause a GPF!

When the content of a dynamic/field string variable is changed, the address of the data will also change (but not the address of the string handle). This means that when the string data is changed, you must refresh any pointers to those strings, as assigned by STRPTR.

Bit and sBit Data Types
PowerBASIC makes two types of data types available to UDT/UNION structures, BIT (unsigned) and SBIT (signed). These data types can contain up to 31 bits and allow for very compact storage of information (0 or 1 values). See PowerBASIC Help for more details.

COM Data Types
The GUID, IAUTOMATION, IDISPATCH, AND IUNKNOWN data types are discussed under the COM tutorial section.

Data Type Reference
A discussion of each data type is provided here, with information about where a data type is typically used. At the end of this page is a summary table of data type information.

Data Type Property Summary
Here is a summary of the data types, the values they can hold, how much memory storage they require (bytes), DEF declaration statement that can define the data type, and the type keyword used in declaration and other statements where the type of a variable must be specified.

If you have any suggestions or corrections, please let me know.