# DataObject class

A **DataObject** is a container for one piece of information held in one or more clipboard formats --- the same payload represented as plain text, Unicode text, RTF, a bitmap, a list of file paths, and so on. The runtime hands a **DataObject** to clipboard and OLE drag-and-drop operations: the source side fills it with [**SetData**](/tB/Packages/VBRUN/DataObject/SetData.md), and the destination side inspects what's available with [**GetFormat**](/tB/Packages/VBRUN/DataObject/GetFormat.md) (or [**AvailableFormats**](/tB/Packages/VBRUN/DataObject/AvailableFormats.md)) and pulls the bytes out with [**GetData**](/tB/Packages/VBRUN/DataObject/GetData.md).

A new **DataObject** is created with **New** and starts out empty.

## Storing and retrieving data

[**SetData**](/tB/Packages/VBRUN/DataObject/SetData.md) places a value into the **DataObject** under a given clipboard format --- typically a value from the **ClipboardConstants** enumeration such as `vbCFText`, `vbCFUnicodeText`, or `vbCFBitmap`. A single object can hold the same logical payload under several formats at once, so consumers with different requirements can each find a representation they understand.

```tb check_build
Dim Data As New DataObject
Data.SetData "Hello, world!", vbCFText
Data.SetData StrConv("Hello, world!", vbUnicode), vbCFUnicodeText
```

[**GetData**](/tB/Packages/VBRUN/DataObject/GetData.md) pulls the value back out for a chosen format. [**Clear**](/tB/Packages/VBRUN/DataObject/Clear.md) removes every format and value at once --- useful when reusing a single **DataObject** for several operations.

twinBASIC also accepts format names as plain strings: [**GetDataByName**](/tB/Packages/VBRUN/DataObject/GetDataByName.md) is the string-keyed counterpart to [**GetData**](/tB/Packages/VBRUN/DataObject/GetData.md), and is convenient for custom or registered formats whose numeric identifier is not known up front.

## Discovering what's there

A consumer that did not place the data itself usually does not know which formats are present. [**GetFormat**](/tB/Packages/VBRUN/DataObject/GetFormat.md) returns **True** if a given clipboard format is available, and [**GetFormatByName**](/tB/Packages/VBRUN/DataObject/GetFormatByName.md) does the same for a named format. To discover the full set, [**AvailableFormats**](/tB/Packages/VBRUN/DataObject/AvailableFormats.md) returns a [**DataObjectFormats**](/tB/Packages/VBRUN/DataObject/DataObjectFormats.md) collection of [**DataObjectFormat**](/tB/Packages/VBRUN/DataObject/DataObjectFormat.md) descriptors --- each with a `Name`, a `FormatType` from **ClipboardConstants**, and information about how the format is stored.

```tb check_build
Dim F As DataObjectFormat
For Each F In Data.AvailableFormats
    Debug.Print F.Name, F.FormatType
Next F
```

> [!NOTE]
> [**AvailableFormats**](/tB/Packages/VBRUN/DataObject/AvailableFormats.md), [**GetFormatByName**](/tB/Packages/VBRUN/DataObject/GetFormatByName.md), and [**GetDataByName**](/tB/Packages/VBRUN/DataObject/GetDataByName.md) are twinBASIC additions; they have no equivalent in VB6.

## Files

When a **DataObject** contains a list of file paths --- for example, the payload of a Windows shell drag-and-drop --- [**Files**](/tB/Packages/VBRUN/DataObject/Files.md) returns a [**DataObjectFiles**](/tB/Packages/VBRUN/DataObject/DataObjectFiles.md) collection holding each path as a **String**.

```tb check_build
Dim Path As Variant
For Each Path In Data.Files
    Debug.Print Path
Next Path
```

## Members

- [AvailableFormats](/tB/Packages/VBRUN/DataObject/AvailableFormats.md) -- returns the collection of formats currently held in the **DataObject** *(twinBASIC extension)*
- [Clear](/tB/Packages/VBRUN/DataObject/Clear.md) -- removes every format and value from the **DataObject**
- [Files](/tB/Packages/VBRUN/DataObject/Files.md) -- returns the collection of file paths held in the **DataObject**
- [GetData](/tB/Packages/VBRUN/DataObject/GetData.md) -- returns the value stored under a given clipboard format
- [GetDataByName](/tB/Packages/VBRUN/DataObject/GetDataByName.md) -- returns the value stored under a given named format *(twinBASIC extension)*
- [GetFormat](/tB/Packages/VBRUN/DataObject/GetFormat.md) -- returns whether the **DataObject** holds a value in a given clipboard format
- [GetFormatByName](/tB/Packages/VBRUN/DataObject/GetFormatByName.md) -- returns whether the **DataObject** holds a value in a given named format *(twinBASIC extension)*
- [SetData](/tB/Packages/VBRUN/DataObject/SetData.md) -- stores a value in the **DataObject** under a given clipboard format
