DataObject.SetData Método

Definição

Adiciona um objeto ao DataObject.

Sobrecargas

Nome Description
SetData(Object)

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

SetData(String, Object)

Adiciona o objeto especificado ao uso do DataObject formato especificado.

SetData(Type, Object)

Adiciona o objeto especificado ao uso do DataObject tipo especificado como o formato.

SetData(String, Boolean, Object)

Adiciona o objeto especificado ao uso do DataObject formato especificado e indica se os dados podem ser convertidos em outro formato.

SetData(Object)

Adiciona o objeto especificado ao uso do DataObject tipo de objeto como o 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 serem armazenados.

Implementações

Exemplos

O exemplo de código a seguir armazena dados em um DataObject. Primeiro, um novo DataObject é criado e um componente é armazenado nele. Em seguida, os dados são recuperados especificando a classe. O resultado é exibido em uma caixa de texto.

Esse código requer que 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

Comentários

Importante

Chamar esse método com dados não confiáveis é um risco à segurança. Chame esse método apenas com dados confiáveis. Para obter mais informações, consulte Validar Todas as Entradas.

Se você não souber o formato do aplicativo de destino, poderá armazenar dados em vários formatos usando esse método. Os dados armazenados usando esse método podem ser convertidos em um formato compatível quando recuperados.

A SetData(Object) sobrecarga armazena o data valor em um formato que ele determina chamando o Object.GetType método. Se data implementar a ISerializable interface, essa sobrecarga também armazenará o valor no Serializable formato.

Confira também

Aplica-se a

SetData(String, Object)

Adiciona o objeto especificado ao uso do DataObject 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. Consulte DataFormats os formatos predefinidos.

data
Object

Os dados a serem armazenados.

Implementações

Exemplos

O exemplo de código a seguir armazena dados em um DataObject, especificando seu formato como Unicode.

Em seguida, os dados são recuperados especificando o formato de texto, pois o padrão é converter os dados quando o formato final é compatível. O resultado é exibido em uma caixa de texto.

Esse código requer que 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

Comentários

Importante

Chamar esse método com dados não confiáveis é um risco à segurança. Chame esse método apenas com dados confiáveis. Para obter mais informações, consulte Validar Todas as Entradas.

Se você não souber o formato do aplicativo de destino, poderá armazenar dados em vários formatos usando esse método.

Os dados armazenados usando esse método podem ser convertidos em um formato compatível quando recuperados.

Confira também

Aplica-se a

SetData(Type, Object)

Adiciona o objeto especificado ao uso do DataObject tipo especificado como o 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

Um Type que representa o formato associado aos dados.

data
Object

Os dados a serem armazenados.

Implementações

Exemplos

O exemplo de código a seguir armazena dados em um DataObject formato de dados usando um Type como formato de dados. Em seguida, os dados são recuperados chamando GetData usando o Type formato de dados para especificar. O resultado é exibido em uma caixa de texto.

Esse código requer que 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

Comentários

Importante

Chamar esse método com dados não confiáveis é um risco à segurança. Chame esse método apenas com dados confiáveis. Para obter mais informações, consulte Validar Todas as Entradas.

Se você não souber o formato do aplicativo de destino, poderá armazenar dados em vários formatos usando esse método.

Os dados armazenados usando esse método podem ser convertidos em um formato compatível quando recuperados.

Confira também

Aplica-se a

SetData(String, Boolean, Object)

Adiciona o objeto especificado ao uso do DataObject formato especificado e indica se os dados podem ser convertidos em 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. Consulte DataFormats os formatos predefinidos.

autoConvert
Boolean

true para permitir que os dados sejam convertidos em outro formato; caso contrário, false.

data
Object

Os dados a serem armazenados.

Implementações

Exemplos

O exemplo de código a seguir armazena dados em um DataObject e especifica que os dados só podem ser recuperados em seu formato nativo.

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

Em seguida, a DataObject consulta é consultada para a lista de formatos de dados disponíveis. Somente o formato Unicode é retornado, embora os dados Unicode possam ser convertidos em texto e em outros formatos.

Esse código requer que 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

Comentários

Importante

Chamar esse método com dados não confiáveis é um risco à segurança. Chame esse método apenas com dados confiáveis. Para obter mais informações, consulte Validar Todas as Entradas.

Se você não souber o formato do aplicativo de destino, poderá armazenar dados em vários formatos usando esse método.

Confira também

Aplica-se a