| Answer: |
For debugging purposes, there are times when it would be nice to be able to visit a Web page and have a listing of all of the application variables and their values. Fortunately this task is relatively simple since the Application object exposes the Contents collection as one of its properties. We can use a For Each ... Next loop to then step through each element of the Contents collection.
Dim strName, iLoop 'Use a For Each ... Next to loop through the entire collection For Each strName in Application.Contents Response.Write strName & " - " & Application.Contents(strName) & "<BR>" Next
|
Pretty straightforward, no? The above code will work flawlessly except when the an array has been assigned to an application variable. Therefore, we need to add an If statement in our For Each ... Next loop to determine whether or not the current application variable is an array. If it is, then we need to step through and display each element of the array.
Here is the revised code that will now gracefully handle/display application-level arrays:
'How many application variables are there? Response.Write "There are " & Application.Contents.Count & _ " Application variables<P>"
Dim strName, iLoop 'Use a For Each ... Next to loop through the entire collection For Each strName in Application.Contents 'Is this application variable an array? If IsArray(Application(strName)) then 'If it is an array, loop through each element one at a time For iLoop = LBound(Application(strName)) to UBound(Application(strName)) Response.Write strName & "(" & iLoop & ") - " & _ Application(strName)(iLoop) & "<BR>" Next Else 'We aren't dealing with an array, so just display the variable Response.Write strName & " - " & Application.Contents(strName) & "<BR>" End If Next
|
And there you have it! A simple loop that will display all application variables, regardless if they are an array or not! If you want to see how to list all the session-level variables, be sure to read the FAQ: How can I list all of the existing Session variables for a particular user?
Happy Programming! |