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.
Example
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);
Description
The following example code is a condensed version of the code shown above.
Code
DataObject dataObject = new DataObject("Some string data to store...");
Example
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);
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...");
Example
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);
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...");
Example
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);
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);