DataObject.SetData Méthode

Définition

Ajoute un objet à DataObject.

Surcharges

SetData(Object)

Ajoute l'objet spécifié au DataObject en utilisant le type d'objet comme format de données.

SetData(String, Object)

Ajoute l'objet spécifié au DataObject à l'aide du format spécifié.

SetData(Type, Object)

Ajoute l'objet spécifié au DataObject en utilisant le type spécifié comme format.

SetData(String, Boolean, Object)

Ajoute l'objet spécifié au DataObject en utilisant le format spécifié et en indiquant si les données peuvent être converties dans un autre format.

SetData(Object)

Ajoute l'objet spécifié au DataObject en utilisant le type d'objet comme format de données.

public:
 virtual void SetData(System::Object ^ data);
public virtual void SetData (object data);
public virtual void SetData (object? data);
abstract member SetData : obj -> unit
override this.SetData : obj -> unit
Public Overridable Sub SetData (data As Object)

Paramètres

data
Object

Données à stocker.

Implémente

Exemples

L’exemple de code suivant stocke des données dans un DataObject. Tout d’abord, un nouveau DataObject est créé et un composant y est stocké. Ensuite, les données sont récupérées en spécifiant la classe . Le résultat s’affiche dans une zone de texte.

Ce code nécessite que ait textBox1 été créé.

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->Name,
           " is present in the DataObject" );
      }
      else
      {
         textBox1->Text = String::Concat( "Data of type ", myType->Name,
           " 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.GetType().Name + 
       " is present in the DataObject";
    else
       textBox1.Text = "Data of type " + myType.GetType().Name +
       " 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.GetType().Name & _
            " is present in the DataObject"
    Else
        textBox1.Text = "Data of type " & myType.GetType().Name & _
            " is not present in the DataObject"
    End If
End Sub

Remarques

Important

L’appel de cette méthode avec des données non approuvées est un risque de sécurité. Appelez cette méthode avec des données approuvées uniquement. Pour plus d’informations, consultez Valider toutes les entrées.

Si vous ne connaissez pas le format de l’application cible, vous pouvez stocker des données dans plusieurs formats à l’aide de cette méthode. Les données stockées à l’aide de cette méthode peuvent être converties dans un format compatible lors de leur récupération.

La SetData(Object) surcharge stocke la data valeur dans un format qu’elle détermine en appelant la Object.GetType méthode . Si data implémente l’interface ISerializable , cette surcharge stocke également la valeur au Serializable format .

Voir aussi

S’applique à

SetData(String, Object)

Ajoute l'objet spécifié au DataObject à l'aide du format spécifié.

public:
 virtual void SetData(System::String ^ format, System::Object ^ data);
public virtual void SetData (string format, object data);
public virtual void SetData (string format, object? data);
abstract member SetData : string * obj -> unit
override this.SetData : string * obj -> unit
Public Overridable Sub SetData (format As String, data As Object)

Paramètres

format
String

Format associé aux données. Consultez DataFormats pour obtenir les formats prédéfinis.

data
Object

Données à stocker.

Implémente

Exemples

L’exemple de code suivant stocke des données dans un DataObject, en spécifiant son format Unicode.

Ensuite, les données sont récupérées en spécifiant le format de texte, car la valeur par défaut consiste à convertir les données lorsque le format final est compatible. Le résultat s’affiche dans une zone de texte.

Ce code nécessite que ait textBox1 été créé.

private:
   void AddMyData()
   {
      // Creates a new data object using a string and the text format.
      DataObject^ myDataObject = gcnew DataObject;
      
      // Stores a string, specifying the Unicode format.
      myDataObject->SetData( DataFormats::UnicodeText, "Text string" );
      
      // Retrieves the data by specifying Text.
      textBox1->Text = myDataObject->GetData( DataFormats::Text )->GetType()->Name;
   }
private void AddMyData() {
    // Creates a new data object using a string and the text format.
    DataObject myDataObject = new DataObject();
 
    // Stores a string, specifying the Unicode format.
    myDataObject.SetData(DataFormats.UnicodeText, "Text string");
 
    // Retrieves the data by specifying Text.
    textBox1.Text = myDataObject.GetData(DataFormats.Text).GetType().Name;
 }
Private Sub AddMyData()
    ' Creates a new data object using a string and the text format.
    Dim myDataObject As New DataObject()
    
    ' Stores a string, specifying the Unicode format.
    myDataObject.SetData(DataFormats.UnicodeText, "Text string")
    
    ' Retrieves the data by specifying Text.
    textBox1.Text = myDataObject.GetData(DataFormats.Text).GetType().Name
End Sub

Remarques

Important

L’appel de cette méthode avec des données non approuvées est un risque de sécurité. Appelez cette méthode avec des données approuvées uniquement. Pour plus d’informations, consultez Valider toutes les entrées.

Si vous ne connaissez pas le format de l’application cible, vous pouvez stocker des données dans plusieurs formats à l’aide de cette méthode.

Les données stockées à l’aide de cette méthode peuvent être converties dans un format compatible lors de leur récupération.

Voir aussi

S’applique à

SetData(Type, Object)

Ajoute l'objet spécifié au DataObject en utilisant le type spécifié comme format.

public:
 virtual void SetData(Type ^ format, System::Object ^ data);
public virtual void SetData (Type format, object data);
public virtual void SetData (Type format, object? data);
abstract member SetData : Type * obj -> unit
override this.SetData : Type * obj -> unit
Public Overridable Sub SetData (format As Type, data As Object)

Paramètres

format
Type

Type représentant le format associé aux données.

data
Object

Données à stocker.

Implémente

Exemples

L’exemple de code suivant stocke des données dans un DataObject à l’aide d’un Type comme format de données. Les données sont ensuite récupérées en appelant GetData à l’aide du Type pour spécifier le format des données. Le résultat s’affiche dans une zone de texte.

Ce code nécessite que ait textBox1 été créé.

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

Remarques

Important

L’appel de cette méthode avec des données non approuvées est un risque de sécurité. Appelez cette méthode avec des données approuvées uniquement. Pour plus d’informations, consultez Valider toutes les entrées.

Si vous ne connaissez pas le format de l’application cible, vous pouvez stocker des données dans plusieurs formats à l’aide de cette méthode.

Les données stockées à l’aide de cette méthode peuvent être converties dans un format compatible lors de leur récupération.

Voir aussi

S’applique à

SetData(String, Boolean, Object)

Ajoute l'objet spécifié au DataObject en utilisant le format spécifié et en indiquant si les données peuvent être converties dans un autre format.

public:
 virtual void SetData(System::String ^ format, bool autoConvert, System::Object ^ data);
public virtual void SetData (string format, bool autoConvert, object data);
public virtual void SetData (string format, bool autoConvert, object? data);
abstract member SetData : string * bool * obj -> unit
override this.SetData : string * bool * obj -> unit
Public Overridable Sub SetData (format As String, autoConvert As Boolean, data As Object)

Paramètres

format
String

Format associé aux données. Consultez DataFormats pour obtenir les formats prédéfinis.

autoConvert
Boolean

true en cas d'autorisation de conversion des données dans un autre format ; sinon false.

data
Object

Données à stocker.

Implémente

Exemples

L’exemple de code suivant stocke des données dans un DataObject et spécifie que les données peuvent uniquement être récupérées dans leur format natif.

Tout d’abord, un nouveau DataObject est créé. Les données au format Unicode sont stockées dans , DataObjectavec autoConvert la valeur définie sur false.

Ensuite, le DataObject est interrogé pour obtenir la liste des formats de données disponibles. Seul le format Unicode est retourné, bien que les données Unicode puissent être converties en texte et en autres formats.

Ce code nécessite que ait textBox1 été créé.

private:
   void AddMyData4()
   {
      // Creates a new data object, and assigns it the component.
      DataObject^ myDataObject = gcnew DataObject;
      
      // Adds data to the DataObject, and specifies no format conversion.
      myDataObject->SetData( DataFormats::UnicodeText, false, "My Unicode data" );
      
      // Gets the data formats in the DataObject.
      array<String^>^ arrayOfFormats = myDataObject->GetFormats();
      
      // Prints the results.
      textBox1->Text = "The format(s) associated with the data are: \n";
      for ( int i = 0; i < arrayOfFormats->Length; i++ )
      {
         textBox1->Text = String::Concat( textBox1->Text, arrayOfFormats[ i ], "\n" );
      }
   }
private void AddMyData4() {
    // Creates a new data object, and assigns it the component.
    DataObject myDataObject = new DataObject();
 
    // Adds data to the DataObject, and specifies no format conversion.
    myDataObject.SetData(DataFormats.UnicodeText, false, "My Unicode data");
 
    // Gets the data formats in the DataObject.
    String[] arrayOfFormats = myDataObject.GetFormats();
 
    // Prints the results.
    textBox1.Text = "The format(s) associated with the data are: " + '\n';
    for(int i=0; i<arrayOfFormats.Length; i++)
       textBox1.Text += arrayOfFormats[i] + '\n';
 }
Private Sub AddMyData4()
    ' Creates a new data object, and assigns it the component.
    Dim myDataObject As New DataObject()
    
    ' Adds data to the DataObject, and specifies no format conversion.
    myDataObject.SetData(DataFormats.UnicodeText, False, "My Unicode data")
    
    ' Gets the data formats in the DataObject.
    Dim arrayOfFormats As String() = myDataObject.GetFormats()
    
    ' Prints the results.
    textBox1.Text = "The format(s) associated with the data are: " & ControlChars.Cr
    Dim i As Integer
    For i = 0 To arrayOfFormats.Length - 1
        textBox1.Text += arrayOfFormats(i) & ControlChars.Cr
    Next i
End Sub

Remarques

Important

L’appel de cette méthode avec des données non approuvées est un risque de sécurité. Appelez cette méthode avec des données approuvées uniquement. Pour plus d’informations, consultez Valider toutes les entrées.

Si vous ne connaissez pas le format de l’application cible, vous pouvez stocker des données dans plusieurs formats à l’aide de cette méthode.

Voir aussi

S’applique à