.NET Framework Class Library
StateBag.Add Method

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.

Namespace: System.Web.UI
Assembly: System.Web (in system.web.dll)

Syntax

Visual Basic (Declaration)
Public Function Add ( _
    key As String, _
    value As Object _
) As StateItem
Visual Basic (Usage)
Dim instance As StateBag
Dim key As String
Dim value As Object
Dim returnValue As StateItem

returnValue = instance.Add(key, value)
C#
public StateItem Add (
    string key,
    Object value
)
C++
public:
StateItem^ Add (
    String^ key, 
    Object^ value
)
J#
public StateItem Add (
    String key, 
    Object value
)
JScript
public function Add (
    key : String, 
    value : Object
) : StateItem

Parameters

key

The attribute name for the StateItem.

value

The value of the item to add to the StateBag.

Return Value

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

Exception typeCondition

ArgumentException

key is a null reference (Nothing in Visual Basic).

- or -

The number of characters in key is 0.

Example

The following code example demonstrates using the Add method.

Visual Basic
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
C#
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."); 
   }
}
JScript
function MovePiece(fromPosition : String, toPosition : String) {
   var bag : StateBag = ViewState;
   var 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."); 
   }
}
Platforms

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

Version Information

.NET Framework

Supported in: 2.0, 1.1, 1.0
See Also

Tags :


Community Content

Michael Freidgeim
Add method is not thread safe.
It seems that Add method is not thread safe and sometimes causes unexpected errors "An entry with the same key already exists."
See the <a href="http://geekswithblogs.net/mnf/archive/2007/07/26/An-error-An-entry-with-the-same-key-already-exists.aspx">blog post</a>
Tags :

Page view tracker