Q(uick)BASIC Statement: TYPE

Quick View

TYPE

A BASIC declaration that defines a data type containing one or more elements

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • TYPE usertype elementname AS typename [elementname AS typename] END TYPE
Description/Parameter(s)
usertype The name of the data type being defined. The name can consist of up to 40 characters and must begin with a letter. Valid characters are A-Z, 0-9, and period (.).
elementname An element of the user-defined data type.
typename The element's type (INTEGER, LONG, SINGLE, DOUBLE, STRING, or a user-defined data type).
Use DIM, REDIM, COMMON, STATIC, or SHARED to create a variable of a user-defined data type.
Example
TYPE Card Suit AS STRING * 9 Value AS INTEGER END TYPE DIM Deck(1 TO 52) AS Card Deck(1).Suit = "Club" Deck(1).Value = 2 PRINT Deck(1).Suit, Deck(1).Value
Syntax
  • TYPE usertype elementname AS typename [elementname AS typename] END TYPE
Description/Parameter(s)
Argument Description
usertype A name given to the user-defined data type. Follows the same rules as a BASIC variable name.
elementname The name of an element of the user-defined data type. Follows the same rules as a BASIC variable name. Cannot be the name of an array.
typename May be any of the following BASIC data types: INTEGER, LONG, SINGLE, DOUBLE, fixed-length string (see note below), or user-defined type.
Note: Strings in user types must be fixed-length strings. String lengths are indicated by an asterisk and a numeric constant. For example, the following line defines an element named Keyword in a user-defined type as a string with length 40:
  • Keyword AS STRING * 40

A user-defined type must be declared in a TYPE declaration before it can be used in the program. Although a user-defined type can only be declared in the module-level code, you may declare a variable to be of a user-defined type anywhere in the module, even in a SUB or FUNCTION.

Use the DIM, REDIM, COMMON, STATIC, or SHARED statements to declare a variable to be of a user-defined type.

Example

TYPE_EX.BAS is a program file in the subdirectory ADVR_EX that illustrates the TYPE...END TYPE statement. To look at the program in the View window and, optionally, to run it, load the program using the File menu's Open Program command.
The program simulates a deck of cards by using a user-defined type. The program builds a deck of cards (an array of user-defined type Card), shuffles the deck, and displays the first five cards.

Syntax
  • TYPE usertype elementname AS typename [elementname AS typename] END TYPE
Description/Parameter(s)
  • The usertype follows the same rules as a BASIC variable name. In the case of an ISAM table, the argument usertype identifies a user-defined table structure.
  • For a data type, elementname follows the same rules as a BASIC variable name. For a table type, elementname follows the ISAM naming conventions.
  • If the argument usertype is a table type, any elementname arguments are the names of columns in the table. The names must be exact matches to existing column names and must follow the ISAM naming conventions .
  • The typename can be a user-defined data type, a nested user-defined type (data types only), or an array.

New Features

  • BASIC now supports:
    • User-defined types for ISAM tables.
    • The currency data type for dollars and cents math.
    • Static arrays in user-defined types.
  • Before you can use an ISAM table, you must declare a record type for the records that make up the table. Instances of this type are used to pass records to and from the table.
  • The following TYPE statement illustrates the use of static arrays. The record StateData includes the CityCode static array, and the record Washington has the same structure as StateData:
    • TYPE StateData
         CityCode(1 TO 100)  AS INTEGER   'declares a static array.
         County AS STRING * 30
      END TYPE
      DIM Washington(1 TO 100) AS StateData
  • When you declare a static array within a user-defined type, its dimensions must be declared with numeric constants rather than variables.
  • For efficiency, make sure that arrays within a user-defined type start on even offsets.
  • You can create very large records when you include static arrays within records. Putting one of these records within a SUB procedure can use large amounts of stack space.

Usage Notes

  • Strings in user types must be fixed-length strings. String lengths are indicated by an asterisk and a numeric constant. For example, the following line defines an element named Keyword in a user-defined type as a string with length 40:
    • TYPE
         keyword AS STRING * 40
      END TYPE
  • A user-defined type must be declared in a TYPE declaration before it can be used in the program. Although a user-defined type can be declared in the module-level code, you can declare a variable to be of a user-defined type anywhere in the module, even in a SUB or FUNCTION.
  • Use the DIM, REDIM, COMMON, STATIC, or SHARED statements to declare a variable to be of a user-defined type.
  • The keyword REM cannot be used as a field name in a TYPE statement. The text that follows is treated as a comment.

ISAM Programming Tip

  • If you have defined a table to have columns A, B, C, and D, you can use a user-defined type that has only the columns you need (any subset of A, B, C, and D).
Example

This example use the PUT statement to store test scores in a random-access file. The TYPE statement is used to create a user-defined variable type.

'Define record fields. TYPE TestRecord NameField AS STRING * 20 ScoreField AS SINGLE END TYPE DIM FileBuffer AS TestRecord DIM I AS LONG 'Open the test data file. OPEN "TESTDAT.DAT" FOR RANDOM AS #1 LEN = LEN(FileBuffer) 'Read pairs of names and scores from the console. CLS 'Clear screen. I = 0 DO I = I + 1 INPUT "Name ? ", FileBuffer.NameField INPUT "Score? ", FileBuffer.ScoreField INPUT "-->More (y/n)? ", Resp$ PUT #1, I, FileBuffer LOOP UNTIL UCASE$(MID$(Resp$, 1, 1)) = "N" PRINT I; " records written." CLOSE #1 'Remove file from disk. KILL "TESTDAT.DAT"