DekGenius.com
[ Team LiB ] Previous Section Next Section

14.3 Collections Reference

The Context object in ASP.NET supports one collection, Items, and an array called AllErrors.

AllErrors

ExceptionArray = Context.AllErrors

Returns an array of Exception objects representing all accumulated errors that occurred in the current request.

As in classic ASP, the Server.GetLastError method returns an ASPError object. This mechanism is still available, though the returned value is now of type Exception rather than ASPError.

Parameters

None.

Example

The example checks to see if the AllErrors array contains any elements and if so, displays them:

Sub Page_Load( )
    Dim i as Integer
    Dim e As New Exception("A generalized error.")
    Context.AddError(e)
    If Not Context.AllErrors Is Nothing Then
        For i = 0 to Context.AllErrors.Length - 1 
            Message.Text = Message.Text & _ 
                "Exception: " & _
                Context.AllErrors(i).ToString( ) & "<br/>"
        Next
    Else
        Message.Text = "No Errors to report."
    End if
End Sub

Notes

Unlike classic ASP, arrays in ASP.NET are zero-based, so the first element in any collection or array will be 0, not 1. Thus, in the example above, the array is indexed from 0 to Length - 1, not from 1 to Length.

Items

Context.Items(Name as String) = Value
Value = Context.Items(Index as Integer)
Value = Context.Items(Name as String)

The Items collection is a key-value collection that can contain any object the developer wishes to save for, at most, the duration of the request. Unlike Session- or Application-level collections that can be used to store values, this collection does not survive past the current request. This collection is the ideal place to store values that need not survive past the current request, especially if the items need to be stored or retrieved in places where the Session or Application objects are not available.

Parameters

Name

The key name for the value to be stored.

Value

The object to be stored in the Items collection, or the value to be retrieved.

Index

The index of the value to be retrieved.

Example

The example adds two values to the Context.Items collection. The first is added traditionally, referring to the key in the Items collection directly. The second is added using the Item's collection's Add method. Finally, the Message label control displays whether the "Foo" value has been set. To display the string key used, you need to use two double quotes, which displays as the literal quote character. In C#, the quote character would need to be escaped by placing a backslash (\) in front of it.

Sub Page_Load( )
   Context.Items("Foo")="Bar"
   Context.Items.Add("Bar","Foo")
   Message.Text = "Context.Items.Contains(""Foo"") is " & _
                  Context.Items.Contains("Foo")
End Sub

Notes

Unlike classic ASP, collections in ASP.NET are zero-based, so the first element in any collection or array is 0, not 1. While you can, as in the example above, use the Add method to add values to the Items collection, this is virtually never done. Values are almost always retrieved by referring to the Items collection directly, either by using a numeric index or indexing using a key string.

    [ Team LiB ] Previous Section Next Section