語言

DataObject.GetDataPresent 方法

定義

判斷儲存在此 DataObject 格式中的資料是否與指定格式相關聯。

多載

名稱 Description
GetDataPresent(String, Boolean)

判斷此檔案 DataObject 是否包含指定格式的資料,或可選擇性地包含可轉換為指定格式的資料。

GetDataPresent(String)

判斷儲存在此 DataObject 中的資料是否與指定格式相關聯或可轉換成該格式。

GetDataPresent(Type)

判斷儲存在此 DataObject 中的資料是否與指定格式相關聯或可轉換成該格式。

GetDataPresent(String, Boolean)

來源:
DataObject.cs
來源:
DataObject.cs
來源:
DataObject.cs
來源:
DataObject.cs
來源:
DataObject.cs

判斷此檔案 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

true 判斷儲存在該格式 DataObject 中的資料是否能轉換成指定格式; 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 :

  • 參數為 autoConvert , true 資料格式可轉換為適當格式。

  • 參數為autoConvertfalse,資料格式正確。

此方法會在以下情況下返回 false :

  • autoConvert參數為 ,true此方法無法找到指定格式的資料,也無法將資料轉換成指定格式,或資料是以自動轉換設定為 false的儲存方式儲存的。

  • 參數為 autoConvert , false 且資料在指定 DataObject 格式中不存在。

Note

如果資料被儲存並指定允許轉換,且所請求的格式與儲存格式相容,則可轉換成其他格式。 例如,儲存在 Unicode 中的資料可以轉換成文字。

Note

若無法取得資料,則不會拋出例外。 相反地, false 將會被退還。

另請參閱

適用於

GetDataPresent(String)

來源:
DataObject.cs
來源:
DataObject.cs
來源:
DataObject.cs
來源:
DataObject.cs
來源:
DataObject.cs

判斷儲存在此 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 預設格式。

傳回

true 若儲存在此 DataObject 中的資料與指定格式相關聯或可轉換為該格式;否則,則為 false。

實作

範例

以下程式碼範例決定目前儲存在此 DataObject 中的資料是否與特定格式相關聯,或是否能轉換成特定格式。 新字 DataObject 串會以字串及其相關格式(指定為文字)初始化。

接著範例會列印文字資料是否存在於 DataObject。 這套規範要求已經建立。textBox1

備註

呼叫此方法以判斷格式是否存在,再呼叫 GetData。 請致電 GetFormats 查詢本 DataObject書中可用的格式。

Note

如果資料儲存時明確允許轉換,且所請求的格式與儲存格式相容,則可轉換成其他格式。 例如,儲存在 Unicode 中的資料可以轉換成文字。

Note

若無法取得資料,則不會拋出例外。 相反地, false 將會被退還。

另請參閱

適用於

GetDataPresent(Type)

來源:
DataObject.cs
來源:
DataObject.cs
來源:
DataObject.cs
來源:
DataObject.cs
來源:
DataObject.cs

判斷儲存在此 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 A 代表需要檢查的格式。

傳回

true 若儲存在此 DataObject 中的資料與指定格式相關聯或可轉換為該格式;否則,則為 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書中可用的格式。

Note

如果資料儲存時明確允許轉換,且所請求的格式與儲存格式相容,則可轉換成其他格式。 例如,儲存在 Unicode 中的資料可以轉換成文字。

Note

若無法取得資料,則不會拋出例外。 相反地, false 將會被退還。

另請參閱

適用於