Getting Started
Introduction
Sample Programs
IDEs
History
Advice
Mini-Tutorial
Tutorials
Code Snippets

Resources
Web Sites
More Tutorials
Forums
Vendors/Tools
Books
Magazines
Newsletters
NewsGroups
User Groups
Talk Shows
Blogs

Controls
Overview
Button
Check3State
Checkbox
ComboBox
Frame
Graphic
Image
ImageX
ImgButton
ImgButtonX
Label
Line
ListBox
ListView
Option
Progress Bar
Scrollbar
StatusBar
TAB
TextBox
Toolbar
TreeView

GBIC >> PowerBASIC >> Tutorials >> Data Types

PowerBASIC Information Center Tutorials
These tutorials were written to help you get a quick, but thorough, understanding of PowerBASIC - the scope of the language as well as it's specific capabilities.

Introduction       Projects       Language           Messages       Functions           Advanced
  • Overview
  • Examples
  • IDE
  • Compilation
  • Distribution
  • Project Files
  • DDT Windows
  • Controls
  • Menus
  • Dialogs  
  • Help Files
  • Resources  
  • Templates  
  • Project Shell  
  • Syntax
  • Operators
  • Data Types
  • Variables
  • Scope
  • Declarations  
  • Procedures
  • Flow Control
  • Windows
  • Messages
  • Callbacks
  • Mouse
  • Keyboard
  • Dialogs
  • Controls
  • Subclassing
  • Arithmetic
  • Trig  
  • Strings
  • Arrays
  • Date/Time
  • Printing
  • Files
  • Folders
  • Keyboard
  • Mouse
  • Sound
  • System
  • Error Traps
  • Debugging
  • Objects
  • Graphics
  • Databases
  • API
  • DLLs
  • ASM
  • Threads
  • 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.

      • Strings
       dynamic, fixed-length, ASCIIZ, field 
      • Integers
       integer, long integer, quad integer 
      • Floating Point  
       single, double, extended
      • Currency
       currency, extended-currency 
      • Unsigned Integers  
       bit, sbit, byte, word, double-word 
      • Pointers
       pointer
      • Multi-Use
       variant
      • Objects
       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:

    • 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.

    • ASCIIZ string
      Fixed-length string ending in $NUL (same as CHR$(0))

    • Byte
      Unsigned integer using 1 byte in memory. Useful for minimizing memory needed to store small values (0-255).

    • Currency
      Floating-point number that maintains 4 digits of precision after. Used for financial calculations. the decimal point.

    • Double-Precision
      Floating-point number with 16 digits of precision. Provides greater range than single precision. Used for decimal numeric calculations needing very large values.

    • Double-Word
      Unsigned integer using 4 bytes (32 bits) in memory. Useful for minimizing memory needed to store very large values (0-4GB). Used to store memory addresses, including pointer values.

    • Extended-Currency
      Floating-point number that maintains 2 digits of precision after the decimal point. Used for financial calculations.

    • Extended-Precision
      Floating-point number with 18 digits of precision. Used for decimal numeric calculations for large numbers (see table below).

    • FIELD String
      Dynamic strings which represent a defined part of a random file buffer or a defined part of a dynamic string. Cannot be part of a UDT or UNION.

    • Fixed-length String
      Strings whose length is defined at compilation time and may not be changed during run time. Required for use in UDT/Union structures. Often assigned values from variable-length strings to truncate or pad results.

    • Integer
      Whole numbers (no decimal point). Can be used for counters or counting in general.

    • Long Integer
      Whole numbers (no decimal point). Used for integer calculations needing large values. But more importantly, is the most efficient numeric type in PowerBASIC. Use long integer instead of integer whenever possible.

    • Pointer
      Contains address in memory where the content of a variable is stored. Used primarily where speed of memory access is important, or where PowerBASIC does not provide another means of accessing the memory content (such as video memory locations).

      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.

    • Quad-Integer
      Whole numbers (no decimal point). Used for integer calculations needing very large values.

    • Single-Precision
      Floating-point number with 6 digits of precision. Used for most decimal numeric calculations within the range (see table below).

    • Variable-Length String
      Strings whose length can be set/changed during run-time, up to 4GB characters. Also called dynamic string.

    • Variant
      More like a container than a simple variable. Can hold any data type - numeric, string, UDT or arrays. Used as COM object parameters. Very inefficient. Cannot be used in an expression, be PRINTed, or used as a member of a UDT/Union. Use only when absolutely necessary, or when the ease of use overrides the efficiency issues.

    • Word
      Unsigned integer using 2 bytes in memory. Useful for minimizing memory needed to store medium values (0-65535).

    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.