# Object
{: .no_toc }

A reference to any object, whose members are looked up when the program runs.

Syntax:

- [**Dim**](/twinBASIC-docs/tB/Core/Dim.md) *varname* **As Object**
- [**Set**](/twinBASIC-docs/tB/Core/Set.md) *varname* **=** *objectexpression*
- [**DefObj**](/twinBASIC-docs/tB/Core/Deftype.md) *letterrange*

An **Object** variable is declared **As Object**. [**DefObj**](/twinBASIC-docs/tB/Core/Deftype.md) makes **Object** the default type of the variables, parameters and functions whose names start with the letters in *letterrange*. **Object** has no type-declaration character. An **Object** variable that is not given a value holds **Nothing**.

An **Object** variable can refer to an object of any class, from a twinBASIC class, a control or a COM component such as `Scripting.Dictionary`. Declaring a variable with a specific class or interface type instead, `Dim Items As Collection`, lets the compiler check the member names when it compiles the program. [**VarType**](/twinBASIC-docs/tB/Modules/Information/VarType.md) of an object variable is **vbObject** (9), and [**TypeName**](/twinBASIC-docs/tB/Modules/Information/TypeName.md) gives the name of the object's class, or `Nothing`.

## Storage and range

An **Object** variable holds a reference to an object, not the object. It takes 4 bytes in a 32-bit build and 8 bytes in a 64-bit build. Two variables can refer to the same object; [**ObjPtr**](/twinBASIC-docs/tB/Modules/Information/ObjPtr.md) gives the address of the object a variable refers to, 0 for **Nothing**.

```tb check_run
Dim Item As Object
Debug.Print TypeName(Item)          ' Nothing
Debug.Print VarType(Item) = vbObject ' True
Debug.Print Item Is Nothing         ' True
Debug.Print ObjPtr(Item)            ' 0
Set Item = New Collection
Debug.Print TypeName(Item)          ' Collection
Debug.Print Item Is Nothing         ' False
Debug.Print IsObject(Item)          ' True
```

## Literals

**Nothing** is the only **Object** literal: it is a reference to no object. An object is made with [**New**](/twinBASIC-docs/tB/Core/New.md), or by a function such as `CreateObject`.

## Assigning

An object reference is assigned with [**Set**](/twinBASIC-docs/tB/Core/Set.md): `Set Item = New Collection`. Without `Set` the assignment goes to the object's default member instead, and fails with error 91, *Object variable or With block variable not set*, when the variable is **Nothing**. `Set Item = Nothing` releases the reference.

[**Is**](/twinBASIC-docs/tB/Core/Is.md) compares two references and tells whether they refer to the same object: `A Is B`, and `A Is Nothing` for a variable with no object.

A reference can be assigned to a variable of a more specific type, `Set Items = Item`, which checks that the object supports that type.

> [!NOTE] BETA 1045
> Assigning an object to a variable of a class or interface it does not support raises the error -2147467262, the **HRESULT** `E_NOINTERFACE`, *No such interface supported*. VB6 raises error 13, *Type mismatch*. Test the object first with `If TypeOf Item Is Collection Then`, or have the handler accept both numbers.

## Late binding
{: index_also="late binding" }

The members of an **Object** variable are not known when the program is compiled. Each call is looked up by name through **IDispatch** when the statement runs, which is *late binding* ([**IDispatch**](/twinBASIC-docs/Reference/COM-Interfaces/IDispatch.md#late-binding-in-twinbasic) describes it). A member that the object does not have raises error 438, *Object doesn't support this property or method*, at that moment, and a member called on **Nothing** raises error 91. A variable declared with a specific class is bound when the program is compiled, and a member the class does not have is a compile error.

```tb check_run
Dim Table As Object
Dim Count As Long
Set Table = CreateObject("Scripting.Dictionary")
Table.Add "first", 1
Debug.Print Table.Count             ' 1
Debug.Print Table("first")          ' 1
On Error Resume Next
Table.NoSuchMember
Debug.Print Err.Number              ' 438
Err.Clear
Set Table = Nothing
Count = Table.Count
Debug.Print Err.Number              ' 91
```

## Example

This example keeps a dictionary in an **Object** variable and tells whether two variables refer to the same dictionary.

```tb check_run
Dim A As Object
Dim B As Object
Set A = CreateObject("Scripting.Dictionary")
Set B = A
B.Add "key", "value"
Debug.Print A.Count                 ' 1
Debug.Print A Is B                  ' True
Set B = CreateObject("Scripting.Dictionary")
Debug.Print A Is B                  ' False
Debug.Print B.Count                 ' 0
```

## See Also

- [Data Types](/twinBASIC-docs/Reference/Data-Types.md#object) -- the summary of all fourteen intrinsic types
- [Variant](/twinBASIC-docs/tB/Core/Variant.md) type -- a **Variant** can hold an object reference too
- [Set](/twinBASIC-docs/tB/Core/Set.md), [New](/twinBASIC-docs/tB/Core/New.md), [Is](/twinBASIC-docs/tB/Core/Is.md) -- statements and operators for object references
- [IDispatch](/twinBASIC-docs/Reference/COM-Interfaces/IDispatch.md) -- how late-bound calls reach an object
- [Deftype](/twinBASIC-docs/tB/Core/Deftype.md) statement
