DataObject.GetData Méthode

Définition

Retourne les données associées au format de données spécifié.

Surcharges

Nom Description
GetData(String, Boolean)

Retourne les données associées au format de données spécifié, à l’aide d’un paramètre de conversion automatisé pour déterminer s’il faut convertir les données au format.

GetData(String)

Retourne les données associées au format de données spécifié.

GetData(Type)

Retourne les données associées au format de type de classe spécifié.

GetData(String, Boolean)

Retourne les données associées au format de données spécifié, à l’aide d’un paramètre de conversion automatisé pour déterminer s’il faut convertir les données au format.

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

Paramètres

format
String

Format des données à récupérer. Consultez DataFormats les formats prédéfinis.

autoConvert
Boolean

true pour convertir les données au format spécifié ; sinon, false.

Retours

Données associées au format spécifié, ou null.

Implémente

Exemples

L’exemple de code suivant récupère les données stockées dans un DataObject, en utilisant le autoConvert paramètre pour spécifier s’il faut convertir le format de données.

Tout d’abord, un nouveau DataObject est créé avec des données de texte. Ensuite, l’exemple tente de récupérer les données, en spécifiant son format en tant que chaîne et sans conversion de format, autrement dit, le autoConvert paramètre est false. Cette opération échoue, car il n’y a pas de données de chaîne dans le DataObject.

Ensuite, l’exemple tente de récupérer à nouveau les données, avec le autoConvert paramètre défini sur true. Cette opération réussit et les résultats sont affichés dans un MessageBox.

Ce code nécessite la textBox1 création.

private:
   void GetMyData3()
   {
      // Creates a new data object using a string and the text format.
      String^ myString = "My new text string";
      DataObject^ myDataObject = gcnew DataObject( DataFormats::Text,myString );
      
      // Prints the string in a text box with autoconvert = false.
      if ( myDataObject->GetData( "System.String", false ) != 0 )
      {
         // Prints the string in a text box.
         textBox1->Text = String::Concat(
            myDataObject->GetData( "System.String", false )->ToString(), "\n" );
      }
      else
      {
         textBox1->Text = "Could not find data of the specified format\n";
      }
      
      // Prints the string in a text box with autoconvert = true.
      textBox1->Text = String::Concat(
            textBox1->Text, myDataObject->GetData( "System.String", true )->ToString() );
   }
private void GetMyData3() {
    // Creates a new data object using a string and the text format.
    string myString = "My new text string";
    DataObject myDataObject = new DataObject(DataFormats.Text, myString);
 
    // Prints the string in a text box with autoconvert = false.
    if(myDataObject.GetData("System.String", false) != null) {
       // Prints the string in a text box.
       textBox1.Text = myDataObject.GetData("System.String", false).ToString() + '\n';
    } else
        {
            textBox1.Text = "Could not find data of the specified format" + '\n';
        }

        // Prints the string in a text box with autoconvert = true.
        textBox1.Text += myDataObject.GetData("System.String", true).ToString();
 }
Private Sub GetMyData3()
    ' Creates a new data object using a string and the text format.
    Dim myString As String = "My new text string"
    Dim myDataObject As New DataObject(DataFormats.Text, myString)
    
    ' Prints the string in a text box with autoconvert = false.
    If (myDataObject.GetData("System.String", False) IsNot Nothing) Then
        ' Prints the string in a text box.
        textBox1.Text = myDataObject.GetData("System.String", False).ToString() & ControlChars.Cr
    Else
        textBox1.Text = "Could not find data of the specified format" & ControlChars.Cr
    End If 
    ' Prints the string in a text box with autoconvert = true.
    textBox1.Text += myDataObject.GetData("System.String", True).ToString()
End Sub

Remarques

Si le autoConvert paramètre est true et que cette méthode ne trouve pas de données dans le format spécifié, elle tente de convertir les données au format. Si les données ne peuvent pas être converties au format spécifié ou si les données ont été stockées avec le jeu falsede conversion automatique en , cette méthode retourne null.

Si le autoConvert paramètre est false, cette méthode retourne des données au format spécifié ou null si aucune donnée de ce format n’est disponible.

Pour déterminer si les données sont associées ou peuvent être converties en un format, un appel GetDataPresent avant d’appeler GetData. Appelez GetFormats une liste de formats valides pour les données stockées dans ce DataObjectfichier .

Note

Les données peuvent être converties dans un autre format s’il a été stocké en spécifiant que cette conversion est autorisée et si le format demandé est compatible avec le format stocké. Par exemple, les données stockées en tant qu’Unicode peuvent être converties en texte.

Lorsque format est Html, cette méthode retourne une chaîne encodée UTF-8 dans les applications qui ciblent .NET 4,5 ou version ultérieure, et une chaîne encodée ANSI dans les applications qui ciblent .NET 4,0 ou inférieur.

Voir aussi

S’applique à

GetData(String)

Retourne les données associées au format de données spécifié.

public:
 virtual System::Object ^ GetData(System::String ^ format);
public virtual object GetData(string format);
abstract member GetData : string -> obj
override this.GetData : string -> obj
Public Overridable Function GetData (format As String) As Object

Paramètres

format
String

Format des données à récupérer. Consultez DataFormats les formats prédéfinis.

Retours

Données associées au format spécifié, ou null.

Implémente

Exemples

L’exemple de code suivant récupère les données stockées dans un DataObject. Tout d’abord, un nouveau DataObject est créé avec des données de texte. Ensuite, les données sont récupérées, en spécifiant son format en tant que chaîne et affichées dans une zone de texte.

Ce code nécessite la textBox1 création.

private:
   void AddMyData3()
   {
      // Creates a component to store in the data object.
      Component^ myComponent = gcnew Component;
      
      // Creates a new data object.
      DataObject^ myDataObject = gcnew DataObject;
      
      // Adds the component to the DataObject.
      myDataObject->SetData( myComponent );
      
      // Prints whether data of the specified type is in the DataObject.
      Type^ myType = myComponent->GetType();
      if ( myDataObject->GetDataPresent( myType ) )
      {
         textBox1->Text = String::Concat( "Data of type ", myType,
            " is present in the DataObject" );
      }
      else
      {
         textBox1->Text = String::Concat( "Data of type ", myType,
            " is not present in the DataObject" );
      }
   }
private void AddMyData3() {
    // Creates a component to store in the data object.
    Component myComponent = new Component();
 
    // Creates a new data object.
    DataObject myDataObject = new DataObject();
 
    // Adds the component to the DataObject.
    myDataObject.SetData(myComponent);
 
    // Prints whether data of the specified type is in the DataObject.
    Type myType = myComponent.GetType();
    if(myDataObject.GetDataPresent(myType))
       textBox1.Text = "Data of type " + myType.ToString() + 
       " is present in the DataObject";
    else
       textBox1.Text = "Data of type " + myType.ToString() +
       " is not present in the DataObject";
 }
Private Sub AddMyData3()
    ' Creates a component to store in the data object.
    Dim myComponent As New Component()
    
    ' Creates a new data object.
    Dim myDataObject As New DataObject()
    
    ' Adds the component to the DataObject.
    myDataObject.SetData(myComponent)
    
    ' Prints whether data of the specified type is in the DataObject.
    Dim myType As Type = myComponent.GetType()
    If myDataObject.GetDataPresent(myType) Then
        textBox1.Text = "Data of type " & myType.ToString() & _
            " is present in the DataObject"
    Else
        textBox1.Text = "Data of type " & myType.ToString() & _
            " is not present in the DataObject"
    End If
End Sub

Remarques

Si cette méthode ne trouve pas de données dans le format spécifié, elle tente de convertir les données au format. Si les données ne peuvent pas être converties au format spécifié ou si les données ont été stockées avec la conversion automatique définie falsesur , cette méthode retourne null.

Pour déterminer si les données sont associées ou peuvent être converties en un format, un appel GetDataPresent avant d’appeler GetData. Appelez GetFormats une liste de formats valides pour les données stockées dans ce DataObjectfichier .

Note

Les données peuvent être converties dans un autre format s’il a été stocké en spécifiant que cette conversion est autorisée et si le format demandé est compatible avec le format stocké. Par exemple, les données stockées en tant qu’Unicode peuvent être converties en texte.

Lorsque format est Html, cette méthode retourne une chaîne encodée UTF-8 dans les applications qui ciblent .NET 4,5 ou version ultérieure, et une chaîne encodée ANSI dans les applications qui ciblent .NET 4,0 ou inférieur.

Voir aussi

S’applique à

GetData(Type)

Retourne les données associées au format de type de classe spécifié.

public:
 virtual System::Object ^ GetData(Type ^ format);
public virtual object GetData(Type format);
abstract member GetData : Type -> obj
override this.GetData : Type -> obj
Public Overridable Function GetData (format As Type) As Object

Paramètres

format
Type

Représentant Type le format des données à récupérer.

Retours

Données associées au format spécifié, ou null.

Implémente

Exemples

L’exemple de code suivant récupère les données stockées dans un DataObject. Tout d’abord, un nouveau DataObject est créé avec un composant. Ensuite, les données sont récupérées, en spécifiant son type. Le type des données récupérées s’affiche dans une zone de texte.

Ce code nécessite la textBox1 création.

private:
   void GetMyData()
   {
      // 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();
      
      // Retrieves the data using myType to represent its type.
      Object^ myObject = myDataObject->GetData( myType );
      if ( myObject != nullptr )
      {
         textBox1->Text = String::Format( "The data type stored in the DataObject is: {0}",
            myObject->GetType()->Name );
      }
      else
      {
         textBox1->Text = "Data of the specified type was not stored in the DataObject.";
      }
   }
private void GetMyData() {
    // 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();
 
    // Retrieves the data using myType to represent its type.
    Object myObject = myDataObject.GetData(myType);
    if(myObject != null)
       textBox1.Text = "The data type stored in the DataObject is: " +
       myObject.GetType().Name;
    else
       textBox1.Text = "Data of the specified type was not stored " +
       "in the DataObject.";
 }
Private Sub GetMyData()
    ' 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()
    
    ' Retrieves the data using myType to represent its type.
    Dim myObject As Object = myDataObject.GetData(myType)
    If (myObject IsNot Nothing) Then
        textBox1.Text = "The data type stored in the DataObject is: " & myObject.GetType().Name
    Else
        textBox1.Text = "Data of the specified type was not stored " & "in the DataObject."
    End If
End Sub

Remarques

Si cette méthode ne trouve pas de données dans le format spécifié, elle tente de convertir les données au format. Si les données ne peuvent pas être converties au format spécifié ou si les données ont été stockées avec la conversion automatique définie falsesur , cette méthode retourne null.

Pour déterminer si les données sont associées ou peuvent être converties en un format, un appel GetDataPresent avant d’appeler GetData. Appelez GetFormats une liste de formats valides pour les données stockées dans ce DataObjectfichier .

Note

Les données peuvent être converties dans un autre format s’il a été stocké en spécifiant que cette conversion est autorisée et si le format demandé est compatible avec le format stocké. Par exemple, les données stockées en tant qu’Unicode peuvent être converties en texte.

Voir aussi

S’applique à