# CType
{: .no_toc }

Performs an explicit type conversion to a type chosen by the caller.

Syntax: **CType(Of** *type* **)** **(** *value* **)**

*type*
: *required* The type to convert *value* to. Any type known to the compiler is accepted, including built-in types, **Enum** types, classes, interfaces, and user-defined types.

*value*
: *required* The expression being converted.

The return type matches *type*.

> [!NOTE]
> **CType** is a twinBASIC extension; VBA has no equivalent.

**CType** has two roles:

1. **As an explicit cast**, used wherever an implicit conversion would either be disallowed or produce a compiler warning. It conveys the same intent as [**CInt**](/tB/Modules/Conversion/CInt.md), [**CLng**](/tB/Modules/Conversion/CLng.md), and the rest of the C-prefix functions, but for any target type --- most usefully when the target is an **Enum** or an interface. For example, assigning a numeric literal or another **Enum** member to an **Enum**-typed variable triggers a compiler warning that **CType** silences:

   ```tb check_build
   Dim day As VbDayOfWeek
   day = CType(Of VbDayOfWeek)(1)
   ```

2. **As a pointer-to-UDT cast**, used to view the memory pointed to by a **LongPtr** as a particular user-defined type without copying it. See [Enhanced Pointer Functionality](/Features/Language/Pointers.md#ctypeof-type) for the canonical examples.

In both roles **CType** is an operator-like form recognized by the compiler; it isn't called like a regular function and the unparameterized name `CType` cannot be assigned to a function reference.

### Common Errors

| Error | When |
|---|---|
| [6 (`&H6`)](/Reference/Run-time-Errors.md#6), *Overflow* | *value* is a number outside the range of *type* |
| [13 (`&HD`)](/Reference/Run-time-Errors.md#13), *Type mismatch* | *value* cannot be converted to *type*, such as a string that does not hold a number converted to a numeric type |
| [94 (`&H5E`)](/Reference/Run-time-Errors.md#94), *Invalid use of Null* | *value* is **Null** and *type* is not **Variant** |

### See Also

- [Enhanced Pointer Functionality](/Features/Language/Pointers.md#ctypeof-type)
- [Generics](/Features/Language/Generics.md)
- [Compiler Warnings](/Features/Compiler-IDE/Compiler-Warnings.md)
- [CBool](/tB/Modules/Conversion/CBool.md), [CByte](/tB/Modules/Conversion/CByte.md), [CInt](/tB/Modules/Conversion/CInt.md), [CLng](/tB/Modules/Conversion/CLng.md), [CDbl](/tB/Modules/Conversion/CDbl.md), [CStr](/tB/Modules/Conversion/CStr.md), [CVar](/tB/Modules/Conversion/CVar.md) functions
