DataObject.GetDataPresent 方法

定义

确定此 DataObject 中存储的数据是否与指定的格式关联。Determines whether data stored in this DataObject is associated with the specified format.

重载

GetDataPresent(String)

确定此 DataObject 中存储的数据是否与指定的格式关联,或是否可以转换成指定的格式。Determines whether data stored in this DataObject is associated with, or can be converted to, the specified format.

GetDataPresent(Type)

确定此 DataObject 中存储的数据是否与指定的格式关联,或是否可以转换成指定的格式。Determines whether data stored in this DataObject is associated with, or can be converted to, the specified format.

GetDataPresent(String, Boolean)

确定此 DataObject 是否包含指定格式的数据,或者是否包含可以转换为指定格式的数据,后者可选。Determines whether this DataObject contains data in the specified format or, optionally, contains data that can be converted to the specified format.

GetDataPresent(String)

确定此 DataObject 中存储的数据是否与指定的格式关联,或是否可以转换成指定的格式。Determines whether data stored in this DataObject is associated with, or can be converted to, the specified format.

public:
 virtual bool GetDataPresent(System::String ^ format);
public virtual bool GetDataPresent (string format);
abstract member GetDataPresent : string -> bool
override this.GetDataPresent : string -> bool
Public Overridable Function GetDataPresent (format As String) As Boolean

参数

format
String

要检查的格式。The format to check for. 请参见 DataFormats 以获取预定义的格式。See DataFormats for predefined formats.

返回

Boolean

如果此 DataObject 中存储的数据与指定的格式关联,或者可以转换成指定的格式,则为 true;否则为 falsetrue if data stored in this DataObject is associated with, or can be converted to, the specified format; otherwise, false.

实现

示例

下面的代码示例确定当前存储在此中的数据 DataObject 是与指定格式关联还是可转换为指定格式。The following code example determines whether data currently stored in this DataObject is associated with, or can be converted to, a specified format. DataObject使用字符串和指定为文本的关联格式来初始化新的。A new DataObject is initialized with a string and its associated format specified as text.

然后,示例输出中是否存在文本数据 DataObjectThen the examples prints whether text data exists in the DataObject. 此代码需要 textBox1 已创建的。This code requires that textBox1 has been created.

注解

调用此方法以确定在调用之前是否存在格式 GetDataCall this method to determine whether a format exists before calling GetData. 调用 GetFormats 此中可用的格式 DataObjectCall GetFormats for the formats that are available in this DataObject.

备注

如果数据已存储,则可以将其转换为另一种格式,这种格式指定允许该转换,并且请求的格式与存储格式兼容。Data can be converted to another format if it was stored specifying that conversion is allowed, and if the requested format is compatible with the stored format. 例如,以 Unicode 形式存储的数据可以转换为文本。For example, data stored as Unicode can be converted to text.

备注

如果无法检索任何数据,则不会引发异常。If no data can be retrieved, no exception will be thrown. 相反, false 将返回。Instead, false will be returned.

另请参阅

适用于

GetDataPresent(Type)

确定此 DataObject 中存储的数据是否与指定的格式关联,或是否可以转换成指定的格式。Determines whether data stored in this DataObject is associated with, or can be converted to, the specified format.

public:
 virtual bool GetDataPresent(Type ^ format);
public virtual bool GetDataPresent (Type format);
abstract member GetDataPresent : Type -> bool
override this.GetDataPresent : Type -> bool
Public Overridable Function GetDataPresent (format As Type) As Boolean

参数

format
Type

表示要检查的格式的 TypeA Type representing the format to check for.

返回

Boolean

如果此 DataObject 中存储的数据与指定的格式关联,或者可以转换成指定的格式,则为 true;否则为 falsetrue if data stored in this DataObject is associated with, or can be converted to, the specified format; otherwise, false.

实现

示例

下面的代码示例确定中是否存在指定类型的数据 DataObject ,或者数据是否可以转换为指定的类型。The following code example determines whether data of the specified type exists in a DataObject, or whether the data can be converted to the specified type. 结果将显示在文本框中。The result is displayed in a text box. 代码需要 textBox1 已创建的。The code requires that textBox1 has been created.

private:
   void GetIfPresent2()
   {
      // Creates a component to store in the data object.
      Component^ myComponent = gcnew Component;
      
      // Creates a new data object and assigns it the component.
      DataObject^ myDataObject = gcnew DataObject( myComponent );
      
      // Creates a type to store the type of data.
      Type^ myType = myComponent->GetType();
      
      // Determines if the DataObject has data of the Type format.
      textBox1->Text = String::Concat( "Is the specified data type available ",
         "in the DataObject? ", myDataObject->GetDataPresent( myType ), "\n" );
      
      // Retrieves the data using its type format, and displays the type.
      Object^ myObject = myDataObject->GetData( myType );
      textBox1->Text = String::Concat( textBox1->Text, "The data type stored ",
         "in the DataObject is: ", myObject->GetType()->Name );
   }
private void GetIfPresent2() {
    // Creates a component to store in the data object.
    Component myComponent = new Component();
 
    // Creates a new data object and assigns it the component.
    DataObject myDataObject = new DataObject(myComponent);
 
    // Creates a type to store the type of data.
    Type myType = myComponent.GetType();
 
    // Determines if the DataObject has data of the Type format.
    textBox1.Text = "Is the specified data type available in the " +
       "DataObject? " + myDataObject.GetDataPresent(myType).ToString() + '\n';
 
    // Retrieves the data using its type format, and displays the type.
    Object myObject = myDataObject.GetData(myType);
    textBox1.Text += "The data type stored in the DataObject is: " +
       myObject.GetType().Name;
 }
 
Private Sub GetIfPresent2()
    ' Creates a component to store in the data object.
    Dim myComponent As New Component()
    
    ' Creates a new data object and assigns it the component.
    Dim myDataObject As New DataObject(myComponent)
    
    ' Creates a type to store the type of data.
    Dim myType As Type = myComponent.GetType()
    
    ' Determines if the DataObject has data of the Type format.
    textBox1.Text = "Is the specified data type available in the " & "DataObject? " & _
        myDataObject.GetDataPresent(myType).ToString() & ControlChars.Cr
    
    ' Retrieves the data using its type format, and displays the type.
    Dim myObject As Object = myDataObject.GetData(myType)
    textBox1.Text += "The data type stored in the DataObject is: " + myObject.GetType().Name
End Sub

注解

调用此方法以确定在调用之前是否存在格式 GetDataCall this method to determine whether a format exists before calling GetData. 调用 GetFormats 此中可用的格式 DataObjectCall GetFormats for the formats that are available in this DataObject.

备注

如果数据已存储,则可以将其转换为另一种格式,这种格式指定允许该转换,并且请求的格式与存储格式兼容。Data can be converted to another format if it was stored specifying that conversion is allowed, and if the requested format is compatible with the stored format. 例如,以 Unicode 形式存储的数据可以转换为文本。For example, data stored as Unicode can be converted to text.

备注

如果无法检索任何数据,则不会引发异常。If no data can be retrieved, no exception will be thrown. 相反, false 将返回。Instead, false will be returned.

另请参阅

适用于

GetDataPresent(String, Boolean)

确定此 DataObject 是否包含指定格式的数据,或者是否包含可以转换为指定格式的数据,后者可选。Determines whether this DataObject contains data in the specified format or, optionally, contains data that can be converted to the specified format.

public:
 virtual bool GetDataPresent(System::String ^ format, bool autoConvert);
public virtual bool GetDataPresent (string format, bool autoConvert);
abstract member GetDataPresent : string * bool -> bool
override this.GetDataPresent : string * bool -> bool
Public Overridable Function GetDataPresent (format As String, autoConvert As Boolean) As Boolean

参数

format
String

要检查的格式。The format to check for. 请参见 DataFormats 以获取预定义的格式。See DataFormats for predefined formats.

autoConvert
Boolean

值为 true 时,确定此 DataObject 中存储的数据是否可以转换成指定的格式;值为 false 时,检查数据的格式是否为指定的格式。true to determine whether data stored in this DataObject can be converted to the specified format; false to check whether the data is in the specified format.

返回

Boolean

如果数据采用指定的格式,或者可以转换成指定的格式,则为 true;否则为 falsetrue if the data is in, or can be converted to, the specified format; otherwise, false.

实现

示例

下面的代码示例确定当前存储在中的数据是否 DataObject 与指定的格式关联。The following code example determines whether data currently stored in the DataObject is associated with a specified format. 首先,使用字符串来初始化新的,并将 DataObject 其格式指定为文本。First, a new DataObject is initialized with a string, specifying its format as text.

然后 DataObject ,将查询与文本格式关联的数据,并将 autoConvert 参数指定为 falseThen the DataObject is queried for data associated with the text format, specifying the autoConvert parameter as false. 此查询的结果将打印在文本框中。The result of this query is printed in a text box.

然后 DataObject ,将查询与字符串格式关联的数据,并将 autoConvert 参数指定为 trueThen the DataObject is queried for data associated with string format, specifying the autoConvert parameter as true. 结果将打印在文本框中。The results are printed in the text box. 此代码需要 textBox1 已创建的。This code requires that textBox1 has been created.

private:
   void GetIfPresent3()
   {
      // Creates a new data object using a string and the text format.
      DataObject^ myDataObject = gcnew DataObject( DataFormats::Text, "Another string" );
      
      // Prints the string in a text box with autoconvert = false.
      if ( myDataObject->GetDataPresent( "System.String", false ) )
      {
         // Prints the string in a text box.
         textBox1->Text = String::Concat(
            myDataObject->GetData( "System.String", false )->ToString(), "\n" );
      }
      else
      {
         textBox1->Text = "Could not convert data to specified format\n";
      }
      
      // Prints the string in a text box with autoconvert = true.
      textBox1->Text = String::Concat( textBox1->Text,
         "With autoconvert = true, you can convert text to string format. String is: ",
         myDataObject->GetData( "System.String", true )->ToString() );
   }
private void GetIfPresent3() {
    // Creates a new data object using a string and the text format.
    DataObject myDataObject = new DataObject(DataFormats.Text, "Another string");
 
    // Prints the string in a text box with autoconvert = false.
    if(myDataObject.GetDataPresent("System.String", false)) {
       // Prints the string in a text box.
       textBox1.Text = myDataObject.GetData("System.String", false).ToString() + '\n';
    } else
        {
            textBox1.Text = "Could not convert data to specified format" + '\n';
        }

        // Prints the string in a text box with autoconvert = true.
        textBox1.Text += "With autoconvert = true, you can convert text to string format. " +
       "String is: " + myDataObject.GetData("System.String", true).ToString();
 }
 
Private Sub GetIfPresent3()
    ' Creates a new data object using a string and the text format.
    Dim myDataObject As New DataObject(DataFormats.Text, "Another string")
    
    ' Prints the string in a text box with autoconvert = false.
    If myDataObject.GetDataPresent("System.String", False) Then
        ' Prints the string in a text box.
        textBox1.Text = myDataObject.GetData("System.String", False).ToString() & ControlChars.Cr
    Else
        textBox1.Text = "Could not convert data to specified format" & ControlChars.Cr
    End If 
    ' Prints the string in a text box with autoconvert = true.
    textBox1.Text &= "With autoconvert = true, you can convert text to string format. " & _
                    "String is: " & myDataObject.GetData("System.String", True).ToString()
End Sub

注解

调用此方法以确定在调用之前是否存在格式 GetDataCall this method to determine whether a format exists before calling GetData. 调用 GetFormats 此中可用的格式 DataObjectCall GetFormats for the formats that are available in this DataObject.

此方法 true 在以下情况下返回:This method returns true when:

  • autoConvert参数为 true ,数据的格式可以转换为适当的格式。The autoConvert parameter is true and the data is in a format that can be converted to the appropriate format.

  • autoConvert参数为 false ,数据采用适当的格式。The autoConvert parameter is false and the data is in the appropriate format.

此方法 false 在以下情况下返回:This method returns false when:

  • autoConvert参数为 true ,并且此方法无法找到具有指定格式的数据,并且它不能将数据转换为指定格式,也不能将数据存储在自动转换设置为的情况下 falseThe autoConvert parameter is true and this method cannot find data in the specified format, and it cannot convert data to the specified format, or the data was stored with automatic conversion set to false.

  • autoConvert参数为 false ,并且数据在指定的格式中不存在于中 DataObjectThe autoConvert parameter is false and data does not exist in this DataObject in the specified format.

备注

如果数据已存储,则可以将其转换为另一种格式,这种格式指定允许该转换,并且请求的格式与存储格式兼容。Data can be converted to another format if it was stored specifying that conversion is allowed and if the requested format is compatible with the stored format. 例如,以 Unicode 形式存储的数据可以转换为文本。For example, data stored as Unicode can be converted to text.

备注

如果无法检索任何数据,则不会引发异常。If no data can be retrieved, no exception will be thrown. 相反, false 将返回。Instead, false will be returned.

另请参阅

适用于