DataObject.GetDataPresent 方法

定義

判斷儲存於這個 DataObject 中的資料是否與所指定格式建立關聯。

多載

GetDataPresent(String)

判斷儲存於這個 DataObject 中的資料是否與所指定格式建立關聯,或是可以轉換成指定的格式。

GetDataPresent(Type)

判斷儲存於這個 DataObject 中的資料是否與所指定格式建立關聯,或是可以轉換成指定的格式。

GetDataPresent(String, Boolean)

判斷這個 DataObject 是否包含指定格式的資料,或者是包含可以轉換成指定格式的資料。

GetDataPresent(String)

判斷儲存於這個 DataObject 中的資料是否與所指定格式建立關聯,或是可以轉換成指定的格式。

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

要檢查的格式。 如需預先定義的格式,請參閱 DataFormats

傳回

如果儲存於這個 DataObject 中的資料與所指定格式建立關聯,或可以轉換成指定的格式,則為 true;否則為 false

實作

範例

下列程式碼範例會判斷目前儲存在這個 DataObject 中的資料是否與指定的格式相關聯,或可以轉換成 。 新的 DataObject 會使用字串及其指定為文字的關聯格式初始化。

然後,這些範例會列印 文字資料是否存在於 中 DataObject 。 此程式碼需要 textBox1 已建立。

備註

呼叫這個方法,以判斷格式是否存在,再呼叫 GetData 。 呼叫 GetFormats 這個 DataObject 中可用的格式。

注意

如果資料儲存指定允許轉換,而且要求的格式與預存格式相容,則可以將資料轉換成另一種格式。 例如,儲存為 Unicode 的資料可以轉換成文字。

注意

如果無法擷取任何資料,則不會擲回任何例外狀況。 相反地, false 將會傳回 。

另請參閱

適用於

GetDataPresent(Type)

判斷儲存於這個 DataObject 中的資料是否與所指定格式建立關聯,或是可以轉換成指定的格式。

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

Type,表示要檢查的格式。

傳回

如果儲存於這個 DataObject 中的資料與所指定格式建立關聯,或可以轉換成指定的格式,則為 true;否則為 false

實作

範例

下列程式碼範例會判斷指定型別的資料是否存在於 DataObject 中,或資料是否可以轉換成指定的型別。 結果會顯示在文字方塊中。 程式碼需要 textBox1 已建立。

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

備註

呼叫這個方法,以判斷格式是否存在,再呼叫 GetData 。 呼叫 GetFormats 這個 DataObject 中可用的格式。

注意

如果資料儲存指定允許轉換,而且要求的格式與預存格式相容,則可以將資料轉換成另一種格式。 例如,儲存為 Unicode 的資料可以轉換成文字。

注意

如果無法擷取任何資料,則不會擲回任何例外狀況。 相反地, false 將會傳回 。

另請參閱

適用於

GetDataPresent(String, Boolean)

判斷這個 DataObject 是否包含指定格式的資料,或者是包含可以轉換成指定格式的資料。

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

要檢查的格式。 如需預先定義的格式,請參閱 DataFormats

autoConvert
Boolean

若要判斷儲存於這個 DataObject 中的資料是否可以轉換成指定的格式,則為 true;若要檢查資料是否為指定的格式,則為 false

傳回

如果資料在指定的格式中,或是可以轉換成指定的格式,則為 true;否則為 false

實作

範例

下列程式碼範例會判斷目前儲存的資料 DataObject 是否與指定的格式相關聯。 首先,新的 DataObject 會使用字串初始化,並將其格式指定為文字。

DataObject然後會查詢 與文字格式相關聯的資料,並將 autoConvert 參數指定為 false 。 此查詢的結果會列印在文字方塊中。

DataObject然後會查詢 與字串格式相關聯的資料,並將 autoConvert 參數指定為 true 。 結果會列印在文字方塊中。 此程式碼需要 textBox1 已建立。

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

備註

呼叫這個方法,以判斷格式是否存在,再呼叫 GetData 。 呼叫 GetFormats 這個 DataObject 中可用的格式。

這個方法會在下列情況下傳 true 回:

  • 參數 autoConverttrue ,且資料的格式可以轉換成適當的格式。

  • 參數 autoConvertfalse ,且資料的格式適當。

這個方法會在下列情況下傳 false 回:

  • 參數 autoConverttrue ,而且這個方法找不到指定格式的資料,而且無法將資料轉換成指定的格式,或資料是以自動轉換設定 false 為 來儲存。

  • 參數 autoConvertfalse ,且資料不存在於 DataObject 指定的格式中。

注意

如果資料儲存指定允許轉換,而且要求的格式與預存格式相容,則可以將資料轉換成另一種格式。 例如,儲存為 Unicode 的資料可以轉換成文字。

注意

如果無法擷取任何資料,則不會擲回任何例外狀況。 相反地, false 將會傳回 。

另請參閱

適用於