Breyta

Deila með


StateBag.Add(String, Object) Method

Definition

Adds a new StateItem object to the StateBag object. If the item already exists in the StateBag object, this method updates the value of the item.

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

Parameters

key
String

The attribute name for the StateItem.

value
Object

The value of the item to add to the StateBag.

Returns

Returns a StateItem that represents the object added to view state.

Exceptions

key is null.

-or-

The number of characters in key is 0.

Examples

The following code example demonstrates using the Add method.

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

Applies to

See also