Share via


StateBag.Add(String, Object) Método

Definición

Agrega un nuevo objeto StateItem al objeto StateBag. Si el elemento ya existe en el objeto StateBag, este método actualiza su valor.

public:
 System::Web::UI::StateItem ^ Add(System::String ^ key, System::Object ^ value);
public System.Web.UI.StateItem Add (string key, object value);
member this.Add : string * obj -> System.Web.UI.StateItem
Public Function Add (key As String, value As Object) As StateItem

Parámetros

key
String

Nombre de atributo para el objeto StateItem.

value
Object

Valor del elemento que se va a agregar a StateBag.

Devoluciones

StateItem

Devuelve un objeto StateItem que representa el objeto agregado al estado de vista.

Excepciones

key es null.

o bien El número de caracteres de key es 0.

Ejemplos

En el ejemplo de código siguiente se muestra el uso del Add método .

void MovePiece(string fromPosition, string toPosition) {
   StateBag bag = ViewState;
   object piece = bag[fromPosition];
   if (piece != null) {
      bag.Remove(fromPosition);
      bag.Add(toPosition, piece);
      RenderBoard();
   }
   else {
      throw new InvalidPositionException("There is no game piece at the \"from\" position."); 
   }
}
Sub MovePiece(fromPosition As String, toPosition As String)
   Dim bag As StateBag = ViewState
   Dim piece As Object = bag(fromPosition)
   If Not (piece Is Nothing) Then
      bag.Remove(fromPosition)
      bag.Add(toPosition, piece)
      RenderBoard()
   Else
      Throw New InvalidPositionException("There is no game piece at the ""from"" position.")
   End If
End Sub 'MovePiece

Se aplica a

Consulte también