Практическое руководство. Перечисление форматов данных в объекте данных
В следующем примере показано, как использовать перегрузки метода GetFormats для получения массива строк, обозначающих каждый доступный в объекте данных формат данных.
Пример
Описание
В следующем примере кода перегрузка GetFormats используется для получения массива строк, обозначающих все форматы данных, доступные в объекте данных (как собственные, так и автоматически преобразуемые).
Код
Dim dataObject As New DataObject("Some string data to store...")
' Get an array of strings, each string denoting a data format
' that is available in the data object. This overload of GetDataFormats
' returns all available data formats, native and auto-convertible.
Dim dataFormats() As String = dataObject.GetFormats()
' Get the number of data formats present in the data object, including both
' auto-convertible and native data formats.
Dim numberOfDataFormats As Integer = dataFormats.Length
' To enumerate the resulting array of data formats, and take some action when
' a particular data format is found, use a code structure similar to the following.
For Each dataFormat As String In dataFormats
If dataFormat = System.Windows.DataFormats.Text Then
' Take some action if/when data in the Text data format is found.
Exit For
ElseIf dataFormat = System.Windows.DataFormats.StringFormat Then
' Take some action if/when data in the string data format is found.
Exit For
End If
Next dataFormat
DataObject dataObject = new DataObject("Some string data to store...");
// Get an array of strings, each string denoting a data format
// that is available in the data object. This overload of GetDataFormats
// returns all available data formats, native and auto-convertible.
string[] dataFormats = dataObject.GetFormats();
// Get the number of data formats present in the data object, including both
// auto-convertible and native data formats.
int numberOfDataFormats = dataFormats.Length;
// To enumerate the resulting array of data formats, and take some action when
// a particular data format is found, use a code structure similar to the following.
foreach (string dataFormat in dataFormats)
{
if (dataFormat == DataFormats.Text)
{
// Take some action if/when data in the Text data format is found.
break;
}
else if(dataFormat == DataFormats.StringFormat)
{
// Take some action if/when data in the string data format is found.
break;
}
}
Пример
Описание
В следующем примере перегрузка GetFormats используется для получения массива строк, обозначающих только форматы данных, доступные в объекте данных (фильтруются автоматически преобразуемые форматы данных).
Код
Dim dataObject As New DataObject("Some string data to store...")
' Get an array of strings, each string denoting a data format
' that is available in the data object. This overload of GetDataFormats
' accepts a Boolean parameter inidcating whether to include auto-convertible
' data formats, or only return native data formats.
Dim dataFormats() As String = dataObject.GetFormats(False) ' Include auto-convertible?
' Get the number of native data formats present in the data object.
Dim numberOfDataFormats As Integer = dataFormats.Length
' To enumerate the resulting array of data formats, and take some action when
' a particular data format is found, use a code structure similar to the following.
For Each dataFormat As String In dataFormats
If dataFormat = System.Windows.DataFormats.Text Then
' Take some action if/when data in the Text data format is found.
Exit For
End If
Next dataFormat
DataObject dataObject = new DataObject("Some string data to store...");
// Get an array of strings, each string denoting a data format
// that is available in the data object. This overload of GetDataFormats
// accepts a Boolean parameter inidcating whether to include auto-convertible
// data formats, or only return native data formats.
string[] dataFormats = dataObject.GetFormats(false /* Include auto-convertible? */);
// Get the number of native data formats present in the data object.
int numberOfDataFormats = dataFormats.Length;
// To enumerate the resulting array of data formats, and take some action when
// a particular data format is found, use a code structure similar to the following.
foreach (string dataFormat in dataFormats)
{
if (dataFormat == DataFormats.Text)
{
// Take some action if/when data in the Text data format is found.
break;
}
}