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.
| dynamic, fixed-length, ASCIIZ, field |
| integer, long integer, quad integer |
| single, double, extended |
| currency, extended-currency |
| bit, sbit, byte, word, double-word |
| pointer |
| variant |
| guid, iautomation, idispatch, iunknown |
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:
|
|
Other Data Types
These other data types are used less frequently, but are nonetheless
very useful in specific types of applications.
|
|
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.
A pointer variable simply contains a 32-bit long integer value. But when prefixed by a @ character, the pointer variable refers to the value of the variable contained at the memory location given by the value of the pointer variable. Pointers-to-pointers can even be created by using a second @ in front of the pointer variable's name.
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.
Type | Specifier | Bytes | Range | DEF type | Keyword |
---|---|---|---|---|---|
Pointer | N/A | 4 | N/A | N/A | PTR/POINTER |
Integer | % | 2 | -32,768 to 32,767 | DEFINT | INTEGER |
Long | & | 4 | -2,147,483,648 to 2,147,483,647 | DEFLNG | LONG |
Quad | && | 8 | -9.22*10^18 to +9.22*10^18 | DEFQUD | QUAD |
Byte | ? | 1 | 0 to 255 | DEFBYT | BYTE |
Word | ?? | 2 | 0 to 65,535 | DEFWRD | WORD |
Double-word | ??? | 4 | 0 to 4,294,967,295 | DEFDWD | DWORD |
Single | ! | 4 | 8.43*10^-37 to 3.40*10^38 | DEFSNG | SINGLE |
Double | # | 8 | 4.19*10^-307 to 1.79*10^308 | DEFDBL | DOUBLE |
Extended-Prec | ## | 10 | 3.4*10^-4932 to 1.2*10^4932 | DEFEXT | EXT/EXTENDED |
Currency | @ | 8 | -9.22*10^14 to +9.22*10^14 | DEFCUR | CUR/CURRENCY |
Extended-Curr | @@ | 8 | -9.22*10^16 to +9.22*10^16 | DEFCUX | CUX/CURRENCYX |
String | $ | 4 | N/A | DEFSTR | STRING |
Fixed String | $ | N/A | N/A | N/A | STRING * x |
ASCIIZ string | N/A | N/A | N/A | N/A | ASCIIZ, ASCIZ |
FIELD string | $ | 16 | N/A | N/A | FIELD |
Variant | N/A | 16 | data dependent | N/A | VARIANT |
GUID | N/A | 16 | N/A | GUID | |
IAUTOMATION | N/A | 4 | N/A | IAUTOMATION | |
IDISPATCH | N/A | 4 | N/A | N/A | IDISPATCH |
IUNKNOWN | N/A | 4 | N/A | IUNKNOWN |
If you have any suggestions or corrections, please let me know.