Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
System.Web.UI
StateBag Class
StateBag Methods
 Add Method

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.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)

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.
Exception typeCondition

ArgumentException

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

- or -

The number of characters in key is 0.

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."); 
   }
}

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.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Add method is not thread safe.      Michael Freidgeim   |   Edit   |   Show History
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 What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker