Skip to content

TYPE (UDT)

Source: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgType Last revised: 2025-03-24

Declares a user-defined type (structure/class).

Syntax

Basic:

Type typename
    fieldname1 As DataType
    fieldname2 As DataType
    ...
End Type

Full (with OOP features):

Type typename [Alias "alternatename"] [Extends base_typename] [Field = alignment]
    [Private:|Public:|Protected:]
    Declare Sub|Function|Constructor|Destructor|Property|Operator ...
    Static variablename As DataType
    Redim arrayname(dimensions) As DataType
    fieldname As DataType [= initializer]
    fieldname(dimensions) As DataType [= initializer]
    fieldname(Any [, Any...]) As DataType
    fieldname : bits As DataType [= initializer]
    [Union]
        fieldname As DataType
        Type
            fieldname As DataType
            ...
        End Type
        ...
    End Union
    Type typename2
        ...
    End Type
    ...
End Type

Description

Type declares a custom data type containing one or more data fields. Supported field types include integers, floats, fixed-size or dynamic arrays, strings, bitfields, and other UDTs.

OOP support:

  • Inheritance: Extends base_typename
  • Member procedures: Sub, Function, Constructor, Destructor, Property, Operator
  • Static member variables
  • Access modifiers: Public:, Private:, Protected:
  • Abstract and virtual methods

Memory layout: Fields are laid out sequentially in memory following native alignment and padding rules. Field = alignment controls field alignment.

Variable-length data: Dynamic strings or array members use descriptors — data is not directly embedded. For embedded data, use fixed-length strings or fixed-size arrays.

Bitfields: Can only be declared inside Type or Union. Bitfield members are packed together.

Nested types: Supports anonymous or named nested Type and Union blocks (since fbc 1.10.0).

Examples

Example 1: QB-style type

vb
Type clr
    red As UByte
    green As UByte
    blue As UByte
End Type

Dim c As clr
c.red = 255
c.green = 128
c.blue = 64

Example 2: Bitfield usage

vb
Function UbyteToOctalString (ByVal b As UByte) As String
    Union UbyteOctal
        number As UByte
        Type
            d0 : 3 As UByte
            d1 : 3 As UByte
            d2 : 2 As UByte
        End Type
    End Union
    Dim uo As UbyteOctal
    uo.number = b
    Return uo.d2 & uo.d1 & uo.d0
End Function

Version

  • Nested named types/unions added since fbc 1.10.0.
  • Fixed-length string behavior changed in fbc 1.20.0 (removed extra null terminator for QB compatibility).

Dialect Differences

  • OOP features require -lang fb dialect.
  • In -lang qb, default byte alignment is used.

Differences from QB

  • Before fbc 1.20.0, fixed-length strings in UDTs used size + 1 bytes (with null terminator), incompatible with QB's file I/O format.
  • Since fbc 1.20.0, fixed-length strings use exactly size bytes (space-padded), compatible with QB.

See Also

Translated from FreeBASIC official docs. Contact us for removal if infringed.
FreeBASIC is an open-source project, not affiliated with Microsoft