Name
Item
Synopsis
Object = Session.Item(ByVal name As String) Session.Item(ByVal name As String) = Object Object = Session.Item(ByVal index As Integer) Session.Item(ByVal index As Integer) = Object
Returns or sets an Object associated with a particular name or index.
Parameters
-
Object
A variable of any type (since all .NET types are ultimately derived from Object) that will receive or set the item’s value.
-
name
A String argument containing the text key to apply to the item or by which to retrieve the item.
-
index
An Integer argument containing the index of the item whose value will be retrieved or modified.
Example
The example sets the values of two items in the Session collection. If these items do not already exist in the collection, they will be added. The example then displays the two values:
Sub Page_Load( ) Session.Item("foo") = "foo" Session.Item("foo2") = "foo2" Message.Text = CStr(Session.Item("foo")) & "</br>" Message.Text &= CStr(Session.Item(1)) End Sub
Notes
The Item property is accessed implicitly when using the syntax:
Session("foo") = "foo"
which is commonly seen in classic ASP code. Using the Item property is not required, but it may make your code more readable and understandable than accessing it implicitly.
Note that an index may be used only as an argument when modifying a value, not to create a new item. The index must also be smaller than the number of items in the Session collection or an exception will be thrown.
Get ASP.NET in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.