# Type
{: .no_toc }

Used at the module level to define a user-defined data type containing one or more elements.

Syntax:
> [ *attributes* ]  
> [ **Private** \| **Public** ] **Type** *varname* [ **(** **Of** *typevars* **)** ]  
> &nbsp;&nbsp;&nbsp;&nbsp; *elementname* [ **(** [ *subscripts* ] **)** ] **As** *type*  
> &nbsp;&nbsp;&nbsp;&nbsp; [ *elementname* [ **(** [ *subscripts* ] **)** ] **As** *type* ]  
> &nbsp;&nbsp;&nbsp;&nbsp; **. . .**  
> &nbsp;&nbsp;&nbsp;&nbsp; [ *member-procedure* ]  
> &nbsp;&nbsp;&nbsp;&nbsp; **. . .**  
> **End Type**

*attributes*
: *optional* (twinBASIC) Type-level attributes. Most notably [**PackingAlignment**](/tB/Core/Attributes.md#packingalignment), which sets the field-alignment value used when laying out the UDT in memory --- useful for interop with C structs declared under `#pragma pack` or `#include <pshpack1.h>`.

**Public**
: *optional* Used to declare user-defined types that are available to all procedures in all modules in all projects.

**Private**
: *optional* Used to declare user-defined types that are available only within the module where the declaration is made.

*varname*
: Name of the user-defined type; follows standard variable naming conventions.

*elementname*
: Name of an element of the user-defined type. Element names also follow standard variable naming conventions, except that keywords can be used.

*subscripts*
: *optional* Dimensions of an array element. When not explicitly stated in *lower*, the lower bound of an array is controlled by the [**Option Base**](/tB/Core/Option.md#Base) statement. The lower bound is zero if no **Option Base** statement is present.

*type*
: Data type of the element; may be **Byte**, **Boolean**, **Integer**, **Long**, **LongLong**, **LongPtr**, **Currency**, **Single**, **Double**, **Decimal**, **Date**, **String** (for variable-length strings), **String** *length* (for fixed-length strings), **Object**, **Variant**, another user-defined type, or an object type. In a generic **Type** (see below), *type* may also be one of the *typevars* introduced in the **Of** clause.

**Of** *typevars*
: *optional* (twinBASIC) One or more type variable names, separated by commas, that make the **Type** a *generic UDT*. Each type variable can be referenced as the *type* of an element. See [Generics](/Features/Language/Generics.md). Generic UDTs do not yet support member procedures.

*member-procedure*
: *optional* (twinBASIC) A [**Sub**](/tB/Core/Sub.md), [**Function**](/tB/Core/Function.md), or [**Property**](/tB/Core/Property.md) procedure, or a [**Declare**](/tB/Core/Declare.md) external procedure, written inside the **Type** body and callable through any variable of that type. See [twinBASIC enhancements](#twinbasic-enhancements) below.

The **Type** statement can be used only at the module level. After a user-defined type has been declared by using the **Type** statement, a variable of that type can be declared anywhere within the scope of the declaration. Use [**Dim**](/tB/Core/Dim.md), [**Private**](/tB/Core/Private.md), [**Public**](/tB/Core/Public.md), [**ReDim**](/tB/Core/ReDim.md), or [**Static**](/tB/Core/Static.md) to declare a variable of a user-defined type.

In standard modules and class modules, user-defined types are public by default. This visibility can be changed by using the **Private** keyword.

Line numbers and line labels aren't allowed in **Type...End Type** blocks.

User-defined types are often used with data records, which frequently consist of a number of related elements of different data types.

The following example shows the use of fixed-size arrays in a user-defined type:

```tb check_build
Type StateData
    CityCode (1 To 100) As Integer    ' Declare a static array.
    County As String * 30
End Type

Dim Washington(1 To 100) As StateData
```

In the preceding example, `StateData` includes the `CityCode` static array, and the record `Washington` has the same structure as `StateData`.

When a fixed-size array is declared within a user-defined type, its dimensions must be declared with numeric literals or constants rather than variables.

### Example

This example uses the **Type** statement to define a user-defined data type. The **Type** statement is used at the module level only. If it appears in a class module, a **Type** statement must be preceded by the keyword **Private**.

```tb check_build
Type EmployeeRecord    ' Create user-defined type.
    ID As Integer    ' Define elements of data type.
    Name As String * 20
    Address As String * 30
    Phone As Long
    HireDate As Date
End Type

Sub CreateRecord()
    Dim MyRecord As EmployeeRecord    ' Declare variable.

    ' Assignment to EmployeeRecord variable must occur in a procedure.
    MyRecord.ID = 12003    ' Assign a value to an element.
End Sub
```

### twinBASIC enhancements

twinBASIC extends classic VBA's **Type** in several ways. UDTs remain stack-allocated structs that can be passed to Win32 APIs, but they can also have behaviour like a lightweight class.

**Member procedures.** [**Sub**](/tB/Core/Sub.md), [**Function**](/tB/Core/Function.md), and [**Property**](/tB/Core/Property.md) procedures may appear inside `Type ... End Type`, and are called through any variable of the type. Inside a member procedure, other members of the same UDT must be accessed with the explicit `Me.` prefix.

> [!WARNING] BETA 1042
> A member procedure called on an element of an array of the UDT, such as `a(1).Sum` or `a(1).Diff(a(2))`, ends the program with an access violation, in 32-bit and 64-bit builds. So does a **Type_Conversion** of an element, explicit or implicit, as in `CStr(a(1))`, `s = a(1)` or `Debug.Print a(1)`. This holds for local, static, module-level, dynamic and parameter arrays, and in a **With** block. A variable of the type, a UDT member of another UDT, and an element of an array declared inside a UDT are not affected, nor is **Type_Assignment** on an element, as in `a(1) = 5`. Copy the element to a variable first.

> [!NOTE] BETA 1042
> Assigning **Me** whole inside a member procedure, as in `Copy = Me`, `Return Me` or `t = Me`, is a code generation error that fails the build once the procedure is called. `Me.X`, passing **Me** as an argument, and `Me = other` are not affected. Copy the members one by one.

> [!WARNING] BETA 1042
> A UDT larger than 4 bytes passed **ByVal** from an element of an array, as in `Take a(1)` for `Sub Take(ByVal p As Pair)`, damages the stack in a 32-bit build: the procedure receives the right value, and the calling procedure ends with an access violation when it returns. A UDT of 4 bytes or less, a variable, an element copied to a variable first, and a **ByRef** parameter are not affected, nor are 64-bit and LLVM builds. VB6 does not accept a UDT passed **ByVal**.

> [!NOTE] BETA 1042
> Assigning without **Set** to a member of a UDT whose type is a class, as in `h.o = 5` for a class with a default member, is a code generation error that fails the build. VB6 assigns the default member. Use `Set h.o = ...` to assign the object, or name the default member, as in `h.o.Value = 5`.

**Lifecycle and operator hooks.** Well-known member names are connected by the compiler:

| Member | Purpose |
|:---|:---|
| `Type_Initialize` | Constructor --- runs when a variable of the type is created. |
| `Type_Terminate` | Destructor --- runs when the variable goes out of scope. |
| `Type_Assignment` | Assignment operator --- `=` assigns *RHS* to a variable of the type. The signature is `Sub Type_Assignment(ByVal RHS As ...)`, and several overloads with different *RHS* types may coexist. |
| `Type_Conversion` | Conversion operator --- produces a value of another type from the UDT. The signature is `Function Type_Conversion() As ...`, and several overloads with different return types may coexist. |
| `Type_DebugView` | Debugger display --- returns a **String** shown in the IDE's variable inspector. |

> [!WARNING] BETA 1042
> **Type_Initialize** does not run for an element of an array of the UDT, or for a UDT that is a member of another UDT. **Type_Terminate** still runs for each of them, on a value that was never initialized, and no error is raised. A variable of the type and a class field of the type are not affected. **ReDim** and **ReDim Preserve** discard elements without running **Type_Terminate**, and an array declared inside a UDT runs neither procedure. Write **Type_Terminate** to do nothing when the members are all zero.

> [!NOTE] BETA 1042
> A UDT with two or more **Type_Conversion** overloads, none of them `As Variant`, cannot be converted to a **Variant**. `Debug.Print c`, `v = c` and `CVar(c)` are a code generation error that fails the build, and an argument for a `ByVal` **Variant** parameter is error TB5001. A conversion to **String** or **Long**, as in `s = c`, is not affected. Name the target type, as in `CStr(c)`, or add an overload `As Variant`, which a conversion to **Variant** then uses.

**API declarations inside a UDT.** A [**Declare**](/tB/Core/Declare.md) statement inside a **Type** body works as a regular module-level declaration, except that when its first parameter is named `Me` and is the same type as the UDT, calls on a variable of that type pass the variable as the first argument implicitly:

```tb check_build slot=module
Type HWND
    Value As LongPtr
    Public Declare PtrSafe Function BringWindowToTop Lib "user32" (ByVal Me As HWND) As Long
End Type

Sub Demo()
    Dim h As HWND
    h.BringWindowToTop()  ' Passes h as the first argument to the API.
End Sub
```

**Custom packing.** The [**PackingAlignment**](/tB/Core/Attributes.md#packingalignment) type-level attribute controls alignment of the UDT's fields. The default packing places each field at a multiple of its own size (with trailing padding so the total size is a multiple of the largest field). Setting `[PackingAlignment(1)]` packs fields with no padding --- matching `#pragma pack(push, 1)` in C.

```tb check_build
[PackingAlignment(2)]
Private Type MyUDT
    x As Integer
    y As Long
    z As Integer
End Type
```

**Generic types.** A type variable list `(Of T)` after the *varname* makes the **Type** generic. Element types may then reference the type variables. Member procedures are not yet supported on generic UDTs.

```tb check_build
Type ListU(Of T)
    value() As T
End Type
```

### See Also

- [**Dim** statement](/tB/Core/Dim.md)
- [**Private** statement](/tB/Core/Private.md)
- [**Public** statement](/tB/Core/Public.md)
- [**Enum** statement](/tB/Core/Enum.md)
- [**Class** statement](/tB/Core/Class.md)
- [UDT Enhancements](/Features/Language/UDTs.md)
- [Generics](/Features/Language/Generics.md)
