TYPE: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "'''TYPE''' definitions are used to create variables that can hold more than one variable type of a fixed byte length. {{PageSyntax}} ::'''TYPE''' typename ::. ::. variable(s) AS type ::. ::'''END TYPE''' * Typename is an undefined type name holder as it can hold any variable types. * TYPE definitions should be placed in the main module before the start of the program code execution. * TYPE definitions CAN be placed in SUB or FUNCTION procedures using QB64...")
 
No edit summary
 
(10 intermediate revisions by 2 users not shown)
Line 3: Line 3:


{{PageSyntax}}
{{PageSyntax}}
::'''TYPE''' typename
:'''TYPE''' typename
 
:.
::.
:. variable(s) [[AS]] type
 
:.
::. variable(s) AS type
:'''END TYPE'''
 
::.
 
::'''END TYPE'''




Line 24: Line 20:
* Type variables use DOT variable names to read or write specific values. They do not use type suffixes as they can hold ANY variable type values! The name before the dot is the one you defined after the type definition and the name after is the variable name used inside of the TYPE. The name of the dimensioned type variable alone can be used to [[PUT]] # or [[GET]] # all of the data at once!
* Type variables use DOT variable names to read or write specific values. They do not use type suffixes as they can hold ANY variable type values! The name before the dot is the one you defined after the type definition and the name after is the variable name used inside of the TYPE. The name of the dimensioned type variable alone can be used to [[PUT]] # or [[GET]] # all of the data at once!
* Once the TYPE variable is created you can find the record or byte size by using [[LEN]](typevariable).
* Once the TYPE variable is created you can find the record or byte size by using [[LEN]](typevariable).
* TYPE definitions can also be placed in [[$INCLUDE]] .BI text files such as [[QB.BI]] is used by [[INTERRUPT]] and [[INTERRUPTX]].  
* TYPE definitions can also be placed in [[$INCLUDE]] .BI text files such as [[QB.BI]] is used by [[INTERRUPT]] and [[INTERRUPTX]].
* '''[[_BIT]] is not currently supported in User Defined [[TYPE]]s'''.
* '''[[_BIT]] is not currently supported in User Defined [[TYPE]]s'''.
* '''NOTE: Many Qbasic keyword variable names CAN be used with a [[STRING]] suffix($) ONLY! You CANNOT use them without the suffix, use a numerical suffix or use [[DIM]], [[REDIM]], [[_DEFINE]], [[BYVAL]] or [[TYPE]] variable [[AS]] statements!'''
* '''NOTE: Many QBasic keyword variable names CAN be used with a [[STRING]] suffix($) ONLY! You CANNOT use them without the suffix, use a numerical suffix or use [[DIM]], [[REDIM]], [[_DEFINE]], [[BYVAL]] or [[TYPE]] variable [[AS]] statements!'''
 
{{DataTypePlugin}}
 
{{DataTypeTable}}




''Example 1:'' Creating a mouse [[INTERRUPT]] TYPE definition. Each [[INTEGER]] value is 2 bytes.
''Example 1:'' Creating a mouse [[INTERRUPT]] TYPE definition. Each [[INTEGER]] value is 2 bytes.
{{CodeStart}}
{{CodeStart}}
  TYPE RegType
  {{Cl|TYPE}} RegType
   AX AS INTEGER    ' mouse function to use
   AX {{Cl|AS}} {{Cl|INTEGER}}   ' mouse function to use
   BX AS INTEGER    ' mouse button
   BX {{Cl|AS}} {{Cl|INTEGER}}   ' mouse button
   CX AS INTEGER    ' mouse graphic column position
   CX {{Cl|AS}} {{Cl|INTEGER}}   ' mouse graphic column position
   DX AS INTEGER    ' mouse graphic row position
   DX {{Cl|AS}} {{Cl|INTEGER}}   ' mouse graphic row position
   BP AS INTEGER    ' not used by mouse, but required *
   BP {{Cl|AS}} {{Cl|INTEGER}}   ' not used by mouse, but required *
   SI AS INTEGER    ' not used by mouse, but required *
   SI {{Cl|AS}} {{Cl|INTEGER}}   ' not used by mouse, but required *
   DI AS INTEGER    ' not used by mouse, but required *
   DI {{Cl|AS}} {{Cl|INTEGER}}   ' not used by mouse, but required *
   Flags AS INTEGER ' not used by mouse but required *
   Flags {{Cl|AS}} {{Cl|INTEGER}} ' not used by mouse but required *
   DS AS INTEGER    ' used by {{Cl|INTERRUPTX}} only
   DS {{Cl|AS}} {{Cl|INTEGER}}   ' used by {{Cl|INTERRUPTX}} only
   ES AS INTEGER    ' used by {{Cl|INTERRUPTX}} only
   ES {{Cl|AS}} {{Cl|INTEGER}}   ' used by {{Cl|INTERRUPTX}} only
  END TYPE
  {{Cl|TYPE|END TYPE}}


  {{Cl|DIM}} {{Cl|SHARED}} InRegs AS RegType, OutRegs AS RegType ' create dot variables
  {{Cl|DIM}} {{Cl|SHARED}} InRegs {{Cl|AS}} RegType, OutRegs {{Cl|AS}} RegType ' create dot variables


  InRegs.AX = 3 ' sets the mouse function to read the mouse buttons and position.
  InRegs.AX = 3 ' sets the mouse function to read the mouse buttons and position.
Line 55: Line 49:
  column% = OutRegs.CX ' returns the current mouse column position
  column% = OutRegs.CX ' returns the current mouse column position
{{CodeEnd}}
{{CodeEnd}}
:''Explanation:'' InRegs and OutRegs become the DOT variable prefix name for the TYPE definition's variables.  
:''Explanation:'' InRegs and OutRegs become the DOT variable prefix name for the TYPE definition's variables.


::::Each TYPE variable is designated as the DOT variable's suffix.
::::Each TYPE variable is designated as the DOT variable's suffix.
Line 76: Line 70:
  END TYPE
  END TYPE


  DIM Contact AS ContactInfo 'create contact record variable for {{Cl|RANDOM}} file  
  DIM Contact AS ContactInfo 'create contact record variable for {{Cl|RANDOM}} file
  RecordLEN% = {{Cl|LEN}}(Contact) ' 118 bytes
  RecordLEN% = {{Cl|LEN}}(Contact) ' 118 bytes
   'define values
   'define values
Line 85: Line 79:
{{CodeEnd}}
{{CodeEnd}}


:''Explanation:'' Use the assigned type variable to find the RANDOM record length which is 118 bytes.  
:''Explanation:'' Use the assigned type variable to find the RANDOM record length which is 118 bytes.
::::The DOT variable names consist of Contact as the prefix:
::::The DOT variable names consist of Contact as the prefix:


Line 104: Line 98:
foobar.c.b = "this is me"
foobar.c.b = "this is me"


PRINT foobar.a, foobar.c.b  
PRINT foobar.a, foobar.c.b
{{Cl|END}} '' ''
{{Cl|END}}
{{CodeEnd}}
{{CodeEnd}}


Line 114: Line 108:
'Bitmap.BI can be included at start of program
'Bitmap.BI can be included at start of program


  TYPE BMPHeaderType        ' Description                  Bytes      '''QB64'''  
  TYPE BMPHeaderType        ' Description                  Bytes      '''QB64'''
   ID AS STRING * 2        ' File ID is "BM"                2  
   ID AS STRING * 2        ' File ID is "BM"                2
   Size AS LONG            ' Size of the data file          4  
   Size AS LONG            ' Size of the data file          4
   Res1 AS INTEGER        ' Reserved 1 should be 0        2  
   Res1 AS INTEGER        ' Reserved 1 should be 0        2
   Res2 AS INTEGER        ' Reserved 2 should be 0        2  
   Res2 AS INTEGER        ' Reserved 2 should be 0        2
   Offset AS LONG          ' Start position of pixel data  4  
   Offset AS LONG          ' Start position of pixel data  4
   Hsize AS LONG          ' Information header size        4  
   Hsize AS LONG          ' Information header size        4
   PWidth AS LONG          ' Image width                    4      {{Cl|_WIDTH (QB64)}}  
   PWidth AS LONG          ' Image width                    4      {{Cb|_WIDTH (function)}}
   PDepth AS LONG          ' Image height                  4      {{Cl|_HEIGHT}}
   PDepth AS LONG          ' Image height                  4      {{Cb|_HEIGHT}}
   Planes AS INTEGER      ' Number of planes              2  
   Planes AS INTEGER      ' Number of planes              2
   BPP AS INTEGER          ' Bits per pixel(palette)        2      {{Cl|_PIXELSIZE}}
   BPP AS INTEGER          ' Bits per pixel(palette)        2      {{Cb|_PIXELSIZE}}
   Compress AS LONG        ' Compression                    4
   Compress AS LONG        ' Compression                    4
   ImageBytes AS LONG      ' Width * Height = ImageSIZE    4
   ImageBytes AS LONG      ' Width * Height = ImageSIZE    4
Line 131: Line 125:
   NumColors AS LONG      ' Number of Colors              4
   NumColors AS LONG      ' Number of Colors              4
   SigColors AS LONG      ' Significant Colors            4
   SigColors AS LONG      ' Significant Colors            4
  END TYPE                  '          Total Header bytes = 54 '' ''
  END TYPE                  '          Total Header bytes = 54
{{TextEnd}}
{{TextEnd}}
{{CodeStart}}
{{CodeStart}}
'{{Cl|$INCLUDE}}: 'Bitmap.BI'  'use only when including a BI file  
'{{Cl|$INCLUDE}}: 'Bitmap.BI'  'use only when including a BI file


{{Cl|DIM}} {{Cl|SHARED}} BMPHead AS BMPHeaderType  
{{Cl|DIM}} {{Cl|SHARED}} BMPHead AS BMPHeaderType


{{Cl|GET|GET #}}1, , BMPHead  'get the entire bitmap header information
{{Cl|GET|GET #}}1, , BMPHead  'get the entire bitmap header information
{{CodeEnd}}
{{CodeEnd}}
:''Explanation:'' Use one [[GET]] to read all of the header information from the start of the bitmap file opened AS [[BINARY]]. It reads all 54 bytes as [[STRING]], [[INTEGER]] and [[LONG]] type DOT variable values.  
:''Explanation:'' Use one [[GET]] to read all of the header information from the start of the bitmap file opened AS [[BINARY]]. It reads all 54 bytes as [[STRING]], [[INTEGER]] and [[LONG]] type DOT variable values.


:NOTE: BPP returns 4(16 colors), 8(256 colors) or 24(16 million colors) bits per pixel in Qbasic. 24 bit can only be in greyscale.
:NOTE: BPP returns 4(16 colors), 8(256 colors) or 24(16 million colors) bits per pixel in QBasic. 24 bit can only be in greyscale.


:Then use the DOT variable name values like this [[GET (graphics statement)]] after you load the bitmap image to the screen:
:Then use the DOT variable name values like this [[GET (graphics statement)]] after you load the bitmap image to the screen:
Line 150: Line 144:
{{CodeEnd}}
{{CodeEnd}}


:The bitmap image is now stored in an {{KW|Arrays|array}} to {{KW|BSAVE}} to a file. The RGB color information follows the file header as [[ASCII]] character values read using {{KW|ASC}}. The color values could be indexed at the start of the Array with the image being offset to: index = NumberOfColors * 3. As determined by the {{KW|SCREEN (statement)|SCREEN}} mode used. In SCREEN 13(256 colors) the index would be 768.
:The bitmap image is now stored in an [[Arrays|array]] to [[BSAVE]] to a file. The RGB color information follows the file header as [[ASCII]] character values read using [[ASC (function)|ASC]]. The color values could be indexed at the start of the Array with the image being offset to: index = NumberOfColors * 3. As determined by the [[SCREEN]] mode used. In SCREEN 13(256 colors) the index would be 768.




''See also:''
{{PageSeeAlso}}
* [[INTEGER]], [[SINGLE]], [[DOUBLE]]
* [[INTEGER]], [[SINGLE]], [[DOUBLE]]
* [[LONG]], [[_INTEGER64]], [[_FLOAT]]
* [[LONG]], [[_INTEGER64]], [[_FLOAT]]
* [[STRING]], [[_BYTE]], [[_BIT]], [[_OFFSET]]
* [[STRING]], [[_BYTE]], [[_BIT]], [[_OFFSET]]
* [[GET|GET #]], [[PUT|PUT #]], [[BINARY]]  
* [[GET|GET #]], [[PUT|PUT #]], [[BINARY]]
* [[GET (graphics statement)]], [[PUT (graphics statement)]]  
* [[GET (graphics statement)]], [[PUT (graphics statement)]]
* [[LEN]], [[LOF]], [[EOF]]
* [[LEN]], [[LOF]], [[EOF]]
* [[Bitmaps]], [[Creating_Icon_Bitmaps#Icon_to_Bitmap_Conversion_Function|Icon to Bitmap Conversion Function]]
* [[Bitmaps]], [[Creating Icon Bitmaps#Icon_to_Bitmap_Conversion_Function|Icon to Bitmap Conversion Function]]




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 23:51, 7 February 2024

TYPE definitions are used to create variables that can hold more than one variable type of a fixed byte length.


Syntax

TYPE typename
.
. variable(s) AS type
.
END TYPE


  • Typename is an undefined type name holder as it can hold any variable types.
  • TYPE definitions should be placed in the main module before the start of the program code execution.
  • TYPE definitions CAN be placed in SUB or FUNCTION procedures using QB64 only!
  • TYPE definitions cannot contain Array variables! Arrays can be DIMensioned as a TYPE definition.
  • TYPE definitions cannot be inside of another TYPE definition, but variables can be defined AS another type.(See Example 3)
  • TYPE definitions must be ended with END TYPE.
  • A TYPE variable MUST be assigned to the type after it is defined. Array variables are allowed.
  • Type variables must be defined in every SUB or FUNCTION unless the type variable is DIMensioned as SHARED.
  • Type variables use DOT variable names to read or write specific values. They do not use type suffixes as they can hold ANY variable type values! The name before the dot is the one you defined after the type definition and the name after is the variable name used inside of the TYPE. The name of the dimensioned type variable alone can be used to PUT # or GET # all of the data at once!
  • Once the TYPE variable is created you can find the record or byte size by using LEN(typevariable).
  • TYPE definitions can also be placed in $INCLUDE .BI text files such as QB.BI is used by INTERRUPT and INTERRUPTX.
  • _BIT is not currently supported in User Defined TYPEs.
  • NOTE: Many QBasic keyword variable names CAN be used with a STRING suffix($) ONLY! You CANNOT use them without the suffix, use a numerical suffix or use DIM, REDIM, _DEFINE, BYVAL or TYPE variable AS statements!
         Table 1: The variable types supported by the QB64 language.
┌───────────────────────────────────────────────────────────────────────────┐
│                              Numerical types                              │
├──────────────────────┬───────────┬────────────────────────────┬───────────┤
│      Type NameTypeMinimum valueSize in  │
│                      │  suffixMaximum valueBytes   │
├──────────────────────┼───────────┼────────────────────────────┼───────────┤
│         _BIT         │     `     │                         -1 │    1/8    │
│                      │           │                          0 │           │
├──────────────────────┼───────────┼────────────────────────────┼───────────┤
│       _BIT * n       │    `n     │                       -128 │    n/8    │
│                      │           │                        127 │           │
├──────────────────────┼───────────┼────────────────────────────┼───────────┤
│    _UNSIGNED _BIT    │    ~`     │                          0 │    1/8    │
│                      │           │                          1 │           │
├──────────────────────┼───────────┼────────────────────────────┼───────────┤
│        _BYTE         │    %%     │                       -128 │     1     │
│                      │           │                        127 │           │
├──────────────────────┼───────────┼────────────────────────────┼───────────┤
│   _UNSIGNED _BYTE    │    ~%%    │                          0 │     1     │
│                      │           │                        255 │           │
├──────────────────────┼───────────┼────────────────────────────┼───────────┤
│       INTEGER        │     %     │                    -32,768 │     2     │
│                      │           │                     32,767 │           │
├──────────────────────┼───────────┼────────────────────────────┼───────────┤
│  _UNSIGNED INTEGER   │    ~%     │                          0 │     2     │
│                      │           │                     65,535 │           │
├──────────────────────┼───────────┼────────────────────────────┼───────────┤
│         LONG         │     &     │             -2,147,483,648 │     4     │
│                      │           │              2,147,483,647 │           │
├──────────────────────┼───────────┼────────────────────────────┼───────────┤
│    _UNSIGNED LONG    │    ~&     │                          0 │     4     │
│                      │           │              4,294,967,295 │           │
├──────────────────────┼───────────┼────────────────────────────┼───────────┤
│      _INTEGER64      │    &&     │ -9,223,372,036,854,775,808 │     8     │
│                      │           │  9,223,372,036,854,775,807 │           │
├──────────────────────┼───────────┼────────────────────────────┼───────────┤
│ _UNSIGNED _INTEGER64 │    ~&&    │                          0 │     8     │
│                      │           │ 18,446,744,073,709,551,615 │           │
├──────────────────────┼───────────┼────────────────────────────┼───────────┤
│                      │           │              -3.402823E+38 │           │
│        SINGLE        │ ! or none │               1.175494E-38 │     4     │
│                      │           │              +3.402823E+38 │           │
├──────────────────────┼───────────┼────────────────────────────┼───────────┤
│                      │           │    -1.797693134862315D+308 │           │
│        DOUBLE        │     #     │     2.225073858507201D-308 │     8     │
│                      │           │    +1.797693134862315D+308 │           │
├──────────────────────┼───────────┼────────────────────────────┼───────────┤
│                      │           │-1.189731495357231765F+4932 │           │
│        _FLOAT        │    ##     │ 3.362103143112093506F-4932 │    32     │
│                      │           │+1.189731495357231765F+4932 │           │
├──────────────────────┼───────────┼────────────────────────────┼───────────┤
│       _OFFSET        │    %&     │ -9,223,372,036,854,775,808 │  use LEN  │
│                      │           │  9,223,372,036,854,775,807 │           │
├──────────────────────┼───────────┼────────────────────────────┼───────────┤
│  _UNSIGNED _OFFSET   │    ~%&    │                          0 │  use LEN  │
│                      │           │ 18,446,744,073,709,551,615 │           │
├──────────────────────┼───────────┼────────────────────────────┴───────────┤
│         _MEM         │   none    │ An internal TYPE like memory variable. │
└──────────────────────┴───────────┴────────────────────────────────────────┘
  For the floating-point numeric types SINGLE (default when not assigned),
DOUBLE and _FLOAT, the middle values represent the smallest possible fraction,
   while the min./max. values above and below represent the limits of the
  possible number range. _OFFSET dot values are used as a part of the _MEM
       variable type in QB64 to return or set the position in memory.

┌───────────────────────────────────────────────────────────────────────────┐
│                                Text types                                 │
├──────────────────────┬───────────┬────────────────────────────┬───────────┤
│      Type NameTypeMinimum valueSize in  │
│                      │  suffixMaximum valueBytes   │
├──────────────────────┼───────────┼────────────────────────────┼───────────┤
│        STRING        │     $     │                          0 │  use LEN  │
│                      │           │              2,147,483,647 │           │
├──────────────────────┼───────────┼────────────────────────────┼───────────┤
│      STRING * n      │    $n     │                          1 │     n     │
│                      │           │              2,147,483,647 │           │
└──────────────────────┴───────────┴────────────────────────────┴───────────┘
  While a regular variable length STRING may have a minimum length of zero
   (empty string), the fixed length string type STRING * n, where n is an
 integer length value, must have a minimum length of at least one character.


Example 1: Creating a mouse INTERRUPT TYPE definition. Each INTEGER value is 2 bytes.

 TYPE RegType
   AX AS INTEGER    ' mouse function to use
   BX AS INTEGER    ' mouse button
   CX AS INTEGER    ' mouse graphic column position
   DX AS INTEGER    ' mouse graphic row position
   BP AS INTEGER    ' not used by mouse, but required *
   SI AS INTEGER    ' not used by mouse, but required *
   DI AS INTEGER    ' not used by mouse, but required *
   Flags AS INTEGER ' not used by mouse but required *
   DS AS INTEGER    ' used by INTERRUPTX only
   ES AS INTEGER    ' used by INTERRUPTX only
 END TYPE

 DIM SHARED InRegs AS RegType, OutRegs AS RegType ' create dot variables

 InRegs.AX = 3 ' sets the mouse function to read the mouse buttons and position.

 CALL INTERRUPT(&H33, InRegs, OutRegs)

 column% = OutRegs.CX ' returns the current mouse column position
Explanation: InRegs and OutRegs become the DOT variable prefix name for the TYPE definition's variables.
Each TYPE variable is designated as the DOT variable's suffix.

* Note: Omitting variables in the RegType definition can change other program variable values!


Example 2: Creating an addressbook database for a RANDOM file.

 TYPE ContactInfo
   First AS STRING * 10
   Last AS STRING * 15
   Address1 AS STRING * 30
   Address2 AS STRING * 30
   City AS STRING * 15
   State AS STRING * 2
   Zip AS LONG   ' (4 bytes)
   Phone AS STRING * 12
 END TYPE

 DIM Contact AS ContactInfo 'create contact record variable for RANDOM file
 RecordLEN% = LEN(Contact) ' 118 bytes
  'define values
 Contact.First = "Ted" ' the fixed string length value will contain 7 extra spaces
 Contact.Zip = 15236 ' LONG value that can be used to search certain zip code numbers.

 PUT #1, 5,Contact  'place contact info into fifth record position
Explanation: Use the assigned type variable to find the RANDOM record length which is 118 bytes.
The DOT variable names consist of Contact as the prefix:


Example 3: Defining a TYPE variable as another variable type from a previous TYPE definition in QB64.

TYPE bar
  b AS STRING * 10
END TYPE

TYPE foo
  a AS SINGLE
  c AS bar          'define variable as a bar type
END TYPE

DIM foobar AS foo   'create a variable to use the foo type
foobar.a = 15.5
foobar.c.b = "this is me"

PRINT foobar.a, foobar.c.b
END


Example 4: A bitmap header information TYPE $INCLUDE File.

' ********
'Bitmap.BI can be included at start of program

 TYPE BMPHeaderType        ' Description                  Bytes      QB64
   ID AS STRING * 2        ' File ID is "BM"                2
   Size AS LONG            ' Size of the data file          4
   Res1 AS INTEGER         ' Reserved 1 should be 0         2
   Res2 AS INTEGER         ' Reserved 2 should be 0         2
   Offset AS LONG          ' Start position of pixel data   4
   Hsize AS LONG           ' Information header size        4
   PWidth AS LONG          ' Image width                    4       _WIDTH (function)
   PDepth AS LONG          ' Image height                   4       _HEIGHT
   Planes AS INTEGER       ' Number of planes               2
   BPP AS INTEGER          ' Bits per pixel(palette)        2       _PIXELSIZE
   Compress AS LONG        ' Compression                    4
   ImageBytes AS LONG      ' Width * Height = ImageSIZE     4
   Xres AS LONG            ' Width in PELS per metre        4
   Yres AS LONG            ' Depth in PELS per metre        4
   NumColors AS LONG       ' Number of Colors               4
   SigColors AS LONG       ' Significant Colors             4
 END TYPE                  '          Total Header bytes = 54
'$INCLUDE: 'Bitmap.BI'  'use only when including a BI file

DIM SHARED BMPHead AS BMPHeaderType

GET #1, , BMPHead  'get the entire bitmap header information
Explanation: Use one GET to read all of the header information from the start of the bitmap file opened AS BINARY. It reads all 54 bytes as STRING, INTEGER and LONG type DOT variable values.
NOTE: BPP returns 4(16 colors), 8(256 colors) or 24(16 million colors) bits per pixel in QBasic. 24 bit can only be in greyscale.
Then use the DOT variable name values like this GET (graphics statement) after you load the bitmap image to the screen:
GET (0, 0)-(BMPHead.PWidth - 1, BMPHead.PDepth - 1), Image(48) 'indexed for 4 BPP colors
The bitmap image is now stored in an array to BSAVE to a file. The RGB color information follows the file header as ASCII character values read using ASC. The color values could be indexed at the start of the Array with the image being offset to: index = NumberOfColors * 3. As determined by the SCREEN mode used. In SCREEN 13(256 colors) the index would be 768.


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link