Count

Integer = Application.Count

 
 

Returns an Integer containing the number of items currently in the Application collection. The Count member is derived from the ICollection interface, which is implemented by the HttpApplicationState class.

 
Parameters
  • Integer
      An Integer variable that will receive the Count property value.
 
Example

The example adds two values to the Application collection, displays the count of items in the Application collection, and then uses the Count property as a looping control value to display each item:

Sub Page_Load( )
	Dim I as Integer
	Application.Clear( )
	Application("foo") = "Hello,"
	Application("bar") = "World!"
	Message.Text = "The Application collection contains" &_
		Application.Count & "items:"
	For I = 0 To Application.Count - 1
		Message.Text &= Application(I)
	Next
End Sub 
Notes

The Count property is new for ASP.NET. In addition to using the Count property for looping through the Application collection, you can use the property to keep track of how many items the Application stores at any given time. For example, you could write this information to a log for later review.

Item

Object = Application.Item(ByVal name As String)
Application.Item(ByVal name As String) = Object
Object = Application.Item(ByVal index As Integer)
Application.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 Application 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( )
	Application.Clear( )
	Application.Item("foo")")=")="foo"
	Application.Item("foo2")")=")="foo2"
	Message.Text = Application.Item("foo") & "  "
	Message.Text &= Application.Item(1)
End Sub
Notes

The Item property is accessed implicitly when using the syntax:

Application("foo") = "foo"

This syntax is often seen in classic ASP code. Explicitly referencing the Item property is not required, but listing it may make your code more readable and understandable than accessing it implicitly. Note that an index may only be used as an argument when modifying a value, not when creating a new item, and the index must be less than the number of items in the Application collection, or an exception will be thrown.

AllKeys

Dim StateVars(Application.Count) As String
StateVars = Application.AllKeys

 
 

Returns a string array of key names stored in the HttpApplicationState object.
 
Parameters
  • StateVars
      A variable of type String array that will receive the array of key names.
 
Example

The example displays all keys of data stored to the Application object:

Sub Page_Load( )
	Dim I as Integer
	Dim StateVars(Application.Count -1) As String
	StateVars = Application.AllKeys
	For I = 0 to StateVars.Length - 1
		Message.Text = Message.Text + StateVars(I) + "< /br >"
	Next I
End Sub
Notes

This property provides a list of key names assigned to all current Application variables.

Contents

HttpApplicationState = Application.Contents

 
 

Returns a reference to the current HttpApplicationState instance.

 
Parameters
  • HttpApplicationState
      A variable of type HttpApplicaitonState that will receive the Contents reference.
 
Example

The example below calls the RemoveAll method through the Contents collection reference and then writes a message:

Sub Page_Load( )
	Application.Contents.RemoveAll( )
	Message.Text = "Removed all items from current Application."
End Sub
Notes

This property is provided for backward compatibility with classic ASP. Properties such as the Item property and methods such as Remove and RemoveAll were accessed via the Contents property in classic ASP. In new ASP.NET development, you should access these members directly. For example, instead of calling the RemoveAll method through the Contents property, you can call RemoveAll method directly:

Application.RemoveAll( )
Keys

KeysCollection = Application.Keys

 
 

Returns a NameObjectCollectionBase.KeysCollection containing the string keys associated with all values stored in the Application collection.

 
Parameters
  • KeysCollection
      A variable of type NameObjectCollectionBase.KeysCollection that will receive the Keys property value.
 
Example

The example loops through the collection of keys in the Application collection, and then displays the key name and the value associated with it by using the Text property of the Message control:

Sub Page_Load( )
	Dim Key As String Message.Text = "Application Keys:"
	For Each Key in Application.Keys
		Message.Text &= "<br/>Key:&nbsp;&nbsp;&nbsp;" & Key
		Message.Text &= "<br/>Value:&nbsp;&nbsp;&nbsp;" &Application(Key)
	Next
End Sub
Notes

The Keys property provides one of many ways to iterate over the contents of the Application collection.

StaticObjects

HttpStaticObjectsCollection = Application.StaticObjects

 
 

Returns an HttpStaticObjectsCollection containing all objects instantiated in global.asax using the <object runat="server"> syntax whose scope attribute is set to Application.

 
Parameters
  • HttpStaticObjectsCollection
      A variable of type HttpStaticObjectsCollection that will receive the StaticObjects property value.
 
Example

The example uses the Count property of the HttpStaticObjectsCollection class to display the number of objects in the current application declared with the <object scope="Application" runat="server"/> syntax in global.asax. It then checks the type of each object, and if it is a Web TextBox control, adds it to the Controls collection of the current page.

Sub Page_Load( )
	Dim myobj As Object
	Message.Text = "There are " & Application.StaticObjects.Count &_
			"objects declared with the" &_
			"&lt;object runat=&quot;server&quot;&gt; syntax " &_
			"in Application scope."
	For Each myObj in Application.StaticObjects
		If myObj.Value.GetType.ToString() =_
			"System.Web.UI.WebControls.TextBox" Then
			Page.Controls.Add(myObj.Value)
		End If
	Next
End Sub
Notes

This property is provided for backward compatibility with classic ASP. You should think carefully before instantiating objects with Session or Application scope because of the impact such objects have on resource usage and application scalability. In most cases, it is advisable to limit objects to page scope. Note that each object in the collection is represented by the DictionaryEntry structure, so its key and value are not directly accessible. To access the key and/or value, use the Key and/or Value members of the DictionaryEntry structure.

Add

Application.Add(ByVal name As String, ByVal value As Object)

 
 

Adds a value to the Application collection.

 
Parameters
  • Name
      A variable of type String that specifies the name of the item to be added to the Application collection.
  • Value
      A variable of type Object that contains the value for the item to be added to the Application collection.
 
Example

The example adds an item to the Application collection and then displays it:

Sub Page_Load( )
	Application.Add("Added", "AddedValue")
	Message.Text = Application("Added")
End Sub
Notes

The Add method, which is new in ASP.NET, provides a technique for adding items to the Application collection that is consistent with the technique used for adding items to other .NET collections. Of course, the classic ASP syntax of directly indexing the Application object by using the key name of index works correctly as well.

Clear

Application.Clear( )

 
 

Clears the contents of the Application collection.

 
Parameters

None

 
Example

The example clears the contents of the Application collection and writes a message to the Text property of the Message control that includes the current count of the collection, which should be 0:

Sub Page_Load( )
	Application.Clear( )
	Message.Text = "There are" & Application.Count &_
		"items in the Application collection."
End Sub
Notes

The Clear method, which is new for ASP.NET, clears only the contents of the Application collection itself. It does not clear the contents of the StaticObjects collection.

Get

Application.Get(ByVal name As String)
Application.Get(ByVal Index As Integer)

 
 

Gets an element of the Application collection either by name or ordinal position (index) within the Application collection. Generally, the name is used in calls to Get unless you need to get members of the collection inside a loop.
 
Parameters
  • Name
      A variable of type String that specifies the name of the item to be retrieved from the Application collection.
  • Index
      A variable of type Integer that specifies the index of the item to be retrieved from the Application collection.
 
Example

The example below sets and gets a value from the Application collection. It also uses the Get method to write a message to the Text property of the Message control that includes the current value of the newly added element of the Application collection.

Sub Page_Load( )
	Application("GetTest") = "Got it!"
	Message.Text = "GetTest = " & Application.Get("GetTest")
End Sub
Notes

You can see whether a named value is saved in the Application collection by checking to ensure that its value is not null, as shown in the following code:

If Not Application("Foo") is Nothing then
	Message.Text = "Foo is set to " & Application.Get("Foo")
End If
GetKey

Application.GetKey(ByVal Index As Integer)

 
 

Retrieves the key name corresponding to the index of a data item stored to the Application object.

 
Parameters
  • Index
      A variable of type Integer that specifies the index of the key to be retrieved from the Application collection.
 
Example

The example removes all values from the Application collection in order to start from a known state. Next, it writes a single value to the Application collection. Finally, it saves the key from the first element (index 0) retrieved by a call to GetKey into the Message control.

Sub Page_Load( )
	Application.RemoveAll( )
	Application("GetKeyTest") ="Got it!"
	Message.Text = "Key of Application(0) = " &_
				   Application.GetKey(0) &_
				   "<br/>(Should be GetKeyTest)"
End Sub
Notes

If Index is less than 0 or greater than Application.Count - 1, an ArgumentOutOfRangeException exception will be thrown.

Lock

Application.Lock

 
 

Locks access to an Application collection to facilitate access synchronization.

 
Parameters

None

 
Example

The example locks the application, sets an application page load counter variable, unlocks the application, and displays the value:

Sub Page_Load( )
	Application.Lock( )
	Application("Counter")= Application("Counter") + 1
	Application.UnLock( )
	Message.Text = "Counter = " & Application("Counter")
End Sub
Notes

In the example, note that we Lock the application, perform any operations that modify values within the Application collection, and UnLock the application as quickly as possible. Any read access to the Application collection can safely take place outside the Lock/UnLock method calls.

Remove

Application.Remove(ByVal name As String)

 
 

Removes an item by name from the Application collection.

 
Parameters
  • Name
      A String argument containing the name (key) of the item to remove.
 
Example

The example determines whether the item with the key "foo" exists in the Application collection and, if it does, removes the item and displays an appropriate message:

Sub Page_Load( )
	If Not Application("foo") Is Nothing Then
		Application.Remove("foo")
		Message.Text = "Item 'foo' was removed."
	Else
		Message.Text = "Item 'foo' does not exist."
	End If
End Sub
Notes

The Remove method is provided for backwards compatibility with classic ASP. In classic ASP, this method was accessed through the Contents collection. In ASP.NET, this method can be accessed either directly, as shown above, or through the Contents collection.

RemoveAll

Application.RemoveAll( )

 
 

Removes all items from the Application collection.

 
Parameters

None

 
Example

The example checks to ensure that at least one item is in the Application collection, and if it is, it clears the collection by calling the RemoveAll method.

Sub Page_Load( )
	If Application.Count > 0 Then
		Application.RemoveAll()
		Message.Text = "Application collection cleared."
	Else
		Message.Text = "Application collection is already empty."
	End If
End Sub
Notes

The RemoveAll method is provided for backwards compatibility with classic ASP. In classic ASP, this method was accessed through the Contents collection. In ASP.NET, this method can be accessed either directly, as shown above, or through the Contents collection.

RemoveAt

Application.RemoveAt(ByVal index As Integer)

 
 

Removes an item from the Application collection by index. This is a new companion to the Remove method, which removes an item by key.

 
Parameters
  • Index
      An Integer argument containing the index location of the item to remove from the Application collection.
 
Example

Sub Page_Load( )
	If Application.Count > 0 Then
		Application.RemoveAt(0)
		Message.Text = "The item at index 0 was removed."
	Else
		Message.Text = "The item at index 0 does not exist."
	End If
End Sub
Notes

The RemoveAt method allows items to be removed from the Application collection by index rather than by key. As in the example above, the items that follow the removed item will shift one position in the collection when the item is removed. If you remove an item by index and then call RemoveAt again with the same index, you will remove the item that immediately followed the original removed item. If a single item is in the Application collection and you call RemoveAt a second time, an ArgumentOutOfRangeException exception will be thrown.

Set

Application.Set(ByVal name As String, ByVal value As Object)

 
 

Updates the value of an object in the Application collection. This new method allows you to set objects in the Application collection.

 
Parameters
  • Name
      A String argument containing the name of the object in the Application collection to be updated.
  • Value
      An Object argument containing the new value of the Application collection object to be updated.
 
Example

The example uses Set twice -- once to set a new item in the Application collection and again to change that value.

Sub Page_Load( )
	Application.RemoveAll( )
	Application.Set("TotallyNewVariable","Test!")
	Message.Text = "First:" + Application("TotallyNewVariable")+ "<br/>"
	Application.Set("TotallyNewVariable","Test again!")
	Message.Text = Message.Text & "First after Set:"+ Application("TotallyNewVariable") + "<br/>"
End Sub
Notes

Set can be used to add values to the Application collection, but you will normally just use the simple syntax you are used to from classic ASP:

Application("TotallyNewVariable") = "Test!"
UnLock

Application.UnLock

 
 

Unlocks access to an Application collection to facilitate access synchronization.

 
Parameters

None

 
Example

The example locks the application, sets an application page load counter variable, unlocks the application, and displays the value:

Sub Page_Load( )
	Application.Lock( )
	Application("Counter") = Application("Counter") + 1
	Application.UnLock( )
	Message.Text = "Counter = " &Application("Counter")
End Sub
Notes

In the example, note that we Lock the application, perform any operations that modify values within the Application collection, and UnLock the application as quickly as possible. Any read access to the Application collection can safely take place outside the Lock/UnLock method calls.

Start
Sub Application_OnStart() 'Event handler logic
End Sub
 
 

Fired when the Application is created. The event handler for this event should be defined in the global.asax application file.
 
Parameters

None

 
Example

The example writes an entry to both the Application Event log and the IIS log for the application to indicate that the Start event has fired:

<Script language="VB" runat="server">
	Sub Application_OnStart()
		Dim EventLog1 As New System.Diagnostics.EventLog ("Application",_
			".", "mySource")
		EventLog1.WriteEntry("Application_OnStart fired!")
		Context.Response.AppendToLog("Application_OnStart fired!")
	End Sub
</script>

There is one issue with the code above. Security in the released version of the .NET framework has been tightened, so writing to the event log will not work by default in an ASP.NET application.

 
Notes

The Start event is useful for performing initialization tasks when the application is initialized. You can initialize Application variables that are mostly static.

End
Sub Application_OnEnd() 'Event handler logic
End Sub
 
 

Fired when the application is torn down, either when the web server is stopped or when the global.asax file is modified. The event handler for this event should be defined in the global.asax application file.
 
Parameters

None

 
Example

The example below writes an entry to the Application Event log to indicate that the End event has fired.

<Script language="VB" runat="server">
	Sub Application_OnEnd()
		Dim EventLog1 As New System.Diagnostics.EventLog ("Application",_
			".", "mySource")
		EventLog1.WriteEntry("Application_OnEnd fired!")
	End Sub
</script>
Notes

The End event is useful for performing cleanup tasks when the Application ends, either because the web service stops or the global.asax file is changed.