DataObject.SetData Method

Definition

Adds an object to the DataObject.

Overloads

SetData(Object)

Adds the specified object to the DataObject using the object type as the data format.

SetData(String, Object)

Adds the specified object to the DataObject using the specified format.

SetData(Type, Object)

Adds the specified object to the DataObject using the specified type as the format.

SetData(String, Boolean, Object)

Adds the specified object to the DataObject using the specified format and indicating whether the data can be converted to another format.

SetData(Object)

Source:
DataObject.cs
Source:
DataObject.cs
Source:
DataObject.cs

Adds the specified object to the DataObject using the object type as the data format.

C#
public virtual void SetData(object data);
C#
public virtual void SetData(object? data);

Parameters

data
Object

The data to store.

Implements

Examples

The following code example stores data in a DataObject. First, a new DataObject is created and a component is stored in it. Then, the data is retrieved by specifying the class. The result is displayed in a text box.

This code requires that textBox1 has been created.

C#
private void AddMyData3() {
    // Creates a component to store in the data object.
    Component myComponent = new Component();
 
    // Creates a new data object.
    DataObject myDataObject = new DataObject();
 
    // Adds the component to the DataObject.
    myDataObject.SetData(myComponent);
 
    // Prints whether data of the specified type is in the DataObject.
    Type myType = myComponent.GetType();
    if(myDataObject.GetDataPresent(myType))
       textBox1.Text = "Data of type " + myType.GetType().Name + 
       " is present in the DataObject";
    else
       textBox1.Text = "Data of type " + myType.GetType().Name +
       " is not present in the DataObject";
 }

Remarks

Important

Calling this method with untrusted data is a security risk. Call this method only with trusted data. For more information, see Validate All Inputs.

If you do not know the format of the target application, you can store data in multiple formats using this method. Data stored using this method can be converted to a compatible format when it is retrieved.

The SetData(Object) overload stores the data value in a format that it determines by calling the Object.GetType method. If data implements the ISerializable interface, this overload also stores the value in the Serializable format.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

SetData(String, Object)

Source:
DataObject.cs
Source:
DataObject.cs
Source:
DataObject.cs

Adds the specified object to the DataObject using the specified format.

C#
public virtual void SetData(string format, object data);
C#
public virtual void SetData(string format, object? data);

Parameters

format
String

The format associated with the data. See DataFormats for predefined formats.

data
Object

The data to store.

Implements

Examples

The following code example stores data in a DataObject, specifying its format as Unicode.

Then the data is retrieved by specifying the text format, since the default is to convert the data when the final format is compatible. The result is displayed in a text box.

This code requires that textBox1 has been created.

C#
private void AddMyData() {
    // Creates a new data object using a string and the text format.
    DataObject myDataObject = new DataObject();
 
    // Stores a string, specifying the Unicode format.
    myDataObject.SetData(DataFormats.UnicodeText, "Text string");
 
    // Retrieves the data by specifying Text.
    textBox1.Text = myDataObject.GetData(DataFormats.Text).GetType().Name;
 }

Remarks

Important

Calling this method with untrusted data is a security risk. Call this method only with trusted data. For more information, see Validate All Inputs.

If you do not know the format of the target application, you can store data in multiple formats using this method.

Data stored using this method can be converted to a compatible format when it is retrieved.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

SetData(Type, Object)

Source:
DataObject.cs
Source:
DataObject.cs
Source:
DataObject.cs

Adds the specified object to the DataObject using the specified type as the format.

C#
public virtual void SetData(Type format, object data);
C#
public virtual void SetData(Type format, object? data);

Parameters

format
Type

A Type representing the format associated with the data.

data
Object

The data to store.

Implements

Examples

The following code example stores data in a DataObject using a Type as the data format. The data is then retrieved by calling GetData using the Type to specify the data format. The result is displayed in a text box.

This code requires that textBox1 has been created.

C#
private void AddMyData2() {
    // Creates a component to store in the data object.
    Component myComponent = new Component();
 
    // Gets the type of the component.
    Type myType = myComponent.GetType();
 
    // Creates a new data object.
    DataObject myDataObject = new DataObject();
 
    // Adds the component to the DataObject.
    myDataObject.SetData(myType, myComponent);
 
    // Prints whether data of the specified type is in the DataObject.
    if(myDataObject.GetDataPresent(myType))
       textBox1.Text = "Data of type " + myType.GetType().Name + 
       " is present in the DataObject";
    else
       textBox1.Text = "Data of type " + myType.GetType().Name +
       " is not present in the DataObject";
 }

Remarks

Important

Calling this method with untrusted data is a security risk. Call this method only with trusted data. For more information, see Validate All Inputs.

If you do not know the format of the target application, you can store data in multiple formats using this method.

Data stored using this method can be converted to a compatible format when it is retrieved.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

SetData(String, Boolean, Object)

Source:
DataObject.cs
Source:
DataObject.cs
Source:
DataObject.cs

Adds the specified object to the DataObject using the specified format and indicating whether the data can be converted to another format.

C#
public virtual void SetData(string format, bool autoConvert, object data);
C#
public virtual void SetData(string format, bool autoConvert, object? data);

Parameters

format
String

The format associated with the data. See DataFormats for predefined formats.

autoConvert
Boolean

true to allow the data to be converted to another format; otherwise, false.

data
Object

The data to store.

Implements

Examples

The following code example stores data in a DataObject and specifies that the data can only be retrieved in its native format.

First, a new DataObject is created. Data in the Unicode format is stored in the DataObject, with autoConvert set to false.

Then, the DataObject is queried for the list of available data formats. Only the Unicode format is returned, although Unicode data can be converted to text and other formats.

This code requires that textBox1 has been created.

C#
private void AddMyData4() {
    // Creates a new data object, and assigns it the component.
    DataObject myDataObject = new DataObject();
 
    // Adds data to the DataObject, and specifies no format conversion.
    myDataObject.SetData(DataFormats.UnicodeText, false, "My Unicode data");
 
    // Gets the data formats in the DataObject.
    String[] arrayOfFormats = myDataObject.GetFormats();
 
    // Prints the results.
    textBox1.Text = "The format(s) associated with the data are: " + '\n';
    for(int i=0; i<arrayOfFormats.Length; i++)
       textBox1.Text += arrayOfFormats[i] + '\n';
 }

Remarks

Important

Calling this method with untrusted data is a security risk. Call this method only with trusted data. For more information, see Validate All Inputs.

If you do not know the format of the target application, you can store data in multiple formats using this method.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9