DataObject.SetData Método

Definição

Adiciona um objeto ao DataObject.

Sobrecargas

Name Description
SetData(Object)

Adiciona o objeto especificado ao DataObject usando o tipo de objeto como formato de dados.

SetData(String, Object)

Adiciona o objeto especificado ao DataObject usando o formato especificado.

SetData(Type, Object)

Adiciona o objeto especificado ao DataObject usando o tipo especificado como formato.

SetData(String, Boolean, Object)

Adiciona o objeto especificado ao DataObject usando o formato especificado e indicando se os dados podem ser convertidos para outro formato.

SetData(Object)

Adiciona o objeto especificado ao DataObject usando o tipo de objeto como formato de dados.

public:
 virtual void SetData(System::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)

Parâmetros

data
Object

Os dados a armazenar.

Implementações

Exemplos

O seguinte exemplo de código armazena dados em um DataObject. Primeiro, um novo DataObject é criado e um componente é armazenado nele. Depois, os dados são recuperados especificando a classe. O resultado é apresentado numa caixa de texto.

Este código exige que isso textBox1 tenha sido criado.

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

Observações

Importante

Chamar este método com dados não confiáveis é um risco de segurança. Chame este método apenas com dados confiáveis. Para obter mais informações, consulte Validar todas as informações inseridas.

Se não souber o formato da aplicação alvo, pode armazenar dados em múltiplos formatos usando este método. Os dados armazenados com este método podem ser convertidos para um formato compatível quando são recuperados.

A SetData(Object) sobrecarga armazena o data valor num formato que determina ao chamar o Object.GetType método. Se data implementar a ISerializable interface, esta sobrecarga também armazena o valor no Serializable formato.

Ver também

Aplica-se a

SetData(String, Object)

Adiciona o objeto especificado ao DataObject usando o formato especificado.

public:
 virtual void SetData(System::String ^ format, System::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)

Parâmetros

format
String

O formato associado aos dados. Veja DataFormats para formatos pré-definidos.

data
Object

Os dados a armazenar.

Implementações

Exemplos

O seguinte exemplo de código armazena dados num DataObject, especificando o seu formato como Unicode.

Depois, os dados são recuperados especificando o formato do texto, já que o padrão é converter os dados quando o formato final for compatível. O resultado é apresentado numa caixa de texto.

Este código exige que isso textBox1 tenha sido criado.

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

Observações

Importante

Chamar este método com dados não confiáveis é um risco de segurança. Chame este método apenas com dados confiáveis. Para obter mais informações, consulte Validar todas as informações inseridas.

Se não souber o formato da aplicação alvo, pode armazenar dados em múltiplos formatos usando este método.

Os dados armazenados com este método podem ser convertidos para um formato compatível quando são recuperados.

Ver também

Aplica-se a

SetData(Type, Object)

Adiciona o objeto especificado ao DataObject usando o tipo especificado como formato.

public:
 virtual void SetData(Type ^ format, System::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)

Parâmetros

format
Type

A Type representa o formato associado aos dados.

data
Object

Os dados a armazenar.

Implementações

Exemplos

O exemplo de código seguinte armazena os dados em a DataObject usando a Type como formato de dados. Os dados são então recuperados ao chamar GetData usando o Type para especificar o formato dos dados. O resultado é apresentado numa caixa de texto.

Este código exige que isso textBox1 tenha sido criado.

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

Observações

Importante

Chamar este método com dados não confiáveis é um risco de segurança. Chame este método apenas com dados confiáveis. Para obter mais informações, consulte Validar todas as informações inseridas.

Se não souber o formato da aplicação alvo, pode armazenar dados em múltiplos formatos usando este método.

Os dados armazenados com este método podem ser convertidos para um formato compatível quando são recuperados.

Ver também

Aplica-se a

SetData(String, Boolean, Object)

Adiciona o objeto especificado ao DataObject usando o formato especificado e indicando se os dados podem ser convertidos para outro formato.

public:
 virtual void SetData(System::String ^ format, bool autoConvert, System::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)

Parâmetros

format
String

O formato associado aos dados. Veja DataFormats para formatos pré-definidos.

autoConvert
Boolean

true para permitir a conversão dos dados para outro formato; caso contrário, false.

data
Object

Os dados a armazenar.

Implementações

Exemplos

O exemplo de código seguinte armazena dados num DataObject e especifica que os dados só podem ser recuperados no seu formato nativo.

Primeiro, é criado um novo DataObject . Os dados no formato Unicode são armazenados no DataObject, com autoConvert definido para false.

Depois, DataObject é consultado para a lista de formatos de dados disponíveis. Apenas o formato Unicode é devolvido, embora os dados Unicode possam ser convertidos em texto e outros formatos.

Este código exige que isso textBox1 tenha sido criado.

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

Observações

Importante

Chamar este método com dados não confiáveis é um risco de segurança. Chame este método apenas com dados confiáveis. Para obter mais informações, consulte Validar todas as informações inseridas.

Se não souber o formato da aplicação alvo, pode armazenar dados em múltiplos formatos usando este método.

Ver também

Aplica-se a