How to: Create a Data Object
The following examples show various ways to create a data object using the constructors provided by the DataObject class.
DataObject(Object) constructor
Description
The following example code creates a new data object and uses one of the overloaded constructors (DataObject(Object)) to initialize the data object with a string. In this case, an appropriate data format is determined automatically according to the stored data's type, and auto-converting of the stored data is allowed by default.
Code
string stringData = "Some string data to store...";
DataObject dataObject = new DataObject(stringData);
Dim stringData As String = "Some string data to store..."
Dim dataObject As New DataObject(stringData)
Description
The following example code is a condensed version of the code shown above.
Code
DataObject dataObject = new DataObject("Some string data to store...");
Dim dataObject As New DataObject("Some string data to store...")
DataObject(String, Object) constructor
Description
The following example code creates a new data object and uses one of the overloaded constructors (DataObject(String, Object)) to initialize the data object with a string and a specified data format. In this case the data format is specified by a string; the DataFormats class provides a set of pre-defined type strings. Auto-converting of the stored data is allowed by default.
Code
string stringData = "Some string data to store...";
string dataFormat = DataFormats.UnicodeText;
DataObject dataObject = new DataObject(dataFormat, stringData);
Dim stringData As String = "Some string data to store..."
Dim dataFormat As String = DataFormats.UnicodeText
Dim dataObject As New DataObject(dataFormat, stringData)
Description
The following example code is a condensed version of the code shown above.
Code
DataObject dataObject = new DataObject(DataFormats.UnicodeText, "Some string data to store...");
Dim dataObject As New DataObject(DataFormats.UnicodeText, "Some string data to store...")
DataObject() constructor
Description
The following example code creates a new data object and uses one of the overloaded constructors (DataObject) to initialize the data object with a string and a specified data format. In this case the data format is specified by a Type parameter. Auto-converting of the stored data is allowed by default.
Code
string stringData = "Some string data to store...";
Type dataFormat = stringData.GetType();
DataObject dataObject = new DataObject(dataFormat, stringData);
Dim stringData As String = "Some string data to store..."
Dim dataFormat As Type = stringData.GetType()
Dim dataObject As New DataObject(dataFormat, stringData)
Description
The following example code is a condensed version of the code shown above.
Code
DataObject dataObject = new DataObject("".GetType(), "Some string data to store...");
Dim dataObject As New DataObject("".GetType(), "Some string data to store...")
DataObject(String, Object, Boolean) constructor
Description
The following example code creates a new data object and uses one of the overloaded constructors (DataObject(String, Object, Boolean)) to initialize the data object with a string and a specified data format. In this case the data format is specified by a string; the DataFormats class provides a set of pre-defined type strings. This particular constructor overload enables the caller to specify whether auto-converting is allowed.
Code
string stringData = "Some string data to store...";
string dataFormat = DataFormats.Text;
bool autoConvert = false;
DataObject dataObject = new DataObject(dataFormat, stringData, autoConvert);
Dim stringData As String = "Some string data to store..."
Dim dataFormat As String = DataFormats.Text
Dim autoConvert As Boolean = False
Dim dataObject As New DataObject(dataFormat, stringData, autoConvert)
Description
The following example code is a condensed version of the code shown above.
Code
DataObject dataObject = new DataObject(DataFormats.Text, "Some string data to store...", false);
Dim dataObject As New DataObject(DataFormats.Text, "Some string data to store...", False)
See also
.NET Desktop feedback