Common ASP.NET Code Techniques (DPC&DWC Reference)--6
lement; line 13 uses the Redirect method of the Response object to send the user to the next element at the top of the stack.
That wraps up our examination of the navigation history stack example. The code samples spanned three listings: Listing 2.1.5, Listing 2.1.6, and Listing 2.1.7. If you decide to use this code on your Web site, there are a couple of things to keep in mind:
First, because our implementation of the navigation history stack is a code snippet in an ASP.NET page, the code in Listing 2.1.5 would need to appear in every Web page on your site. This, of course, is a ridiculous requirement; it would make sense to encapsulate the code and functionality in a user control to allow for easy code reuse. (For more information on user controls, refer to Chapter 5, "Creating and Using User Controls.")
Second, remember that in Back.CSharp.aspx we are Popping off the top two URLs. Because Pop removes these elements from the Stack altogether, the navigation history stack cannot contain any sort of Forward link.
Similarities Among the Collection Types
Because each collection has the same basic functionality—to serve as a variable-sized storage medium for Objects—it is not surprising that the collection types have much in common with one another. All have methods to add and remove elements from the collection. The Count property, which returns the total number of elements in the collection, is common among all collection types.
Each collection also has a means to iterate through each element. This can be accomplished in VB using a For Each ... Next loop or, in C#, a foreach loop, as follows:
'With VB, use a For Each ... Next Loop
Dim qTasks as Queue = New Queue()
' ... Populate the Queue ...
Dim s as String
For Each s in qTasks
's represents the current element in qTasks
Response.Write(s + "<br>")
Next
// In C#, a foreach construct can be used to iterate
// through each element
Queue qTasks = new Queue();
// ... Populate the Queue ...
foreach (String s in qTasks)
{
// s represents the current element in qTasks
Response.Write(s + "<br>");
}
Although each collection can be iterated via a For Each ... Next or foreach loop, each collection can also have its elements iterated with an enumerator. Enumerators are small classes that provide a simple functionality: to serve as a (read-only) cursor to allow the developer to step through the elements of a collection.
The .NET Framework provides a number of specific enumerators for specific collection types. For example,the IDictionaryElement enumerator is useful for iterating through a Hashtable. The IList enumerator is handy for stepping through the elements of an ArrayList. All these specialized enumerators are derived from a base enumerator interface, IEnumerator. Because of this fact, all the collectio
That wraps up our examination of the navigation history stack example. The code samples spanned three listings: Listing 2.1.5, Listing 2.1.6, and Listing 2.1.7. If you decide to use this code on your Web site, there are a couple of things to keep in mind:
First, because our implementation of the navigation history stack is a code snippet in an ASP.NET page, the code in Listing 2.1.5 would need to appear in every Web page on your site. This, of course, is a ridiculous requirement; it would make sense to encapsulate the code and functionality in a user control to allow for easy code reuse. (For more information on user controls, refer to Chapter 5, "Creating and Using User Controls.")
Second, remember that in Back.CSharp.aspx we are Popping off the top two URLs. Because Pop removes these elements from the Stack altogether, the navigation history stack cannot contain any sort of Forward link.
Similarities Among the Collection Types
Because each collection has the same basic functionality—to serve as a variable-sized storage medium for Objects—it is not surprising that the collection types have much in common with one another. All have methods to add and remove elements from the collection. The Count property, which returns the total number of elements in the collection, is common among all collection types.
Each collection also has a means to iterate through each element. This can be accomplished in VB using a For Each ... Next loop or, in C#, a foreach loop, as follows:
'With VB, use a For Each ... Next Loop
Dim qTasks as Queue = New Queue()
' ... Populate the Queue ...
Dim s as String
For Each s in qTasks
's represents the current element in qTasks
Response.Write(s + "<br>")
Next
// In C#, a foreach construct can be used to iterate
// through each element
Queue qTasks = new Queue();
// ... Populate the Queue ...
foreach (String s in qTasks)
{
// s represents the current element in qTasks
Response.Write(s + "<br>");
}
Although each collection can be iterated via a For Each ... Next or foreach loop, each collection can also have its elements iterated with an enumerator. Enumerators are small classes that provide a simple functionality: to serve as a (read-only) cursor to allow the developer to step through the elements of a collection.
The .NET Framework provides a number of specific enumerators for specific collection types. For example,the IDictionaryElement enumerator is useful for iterating through a Hashtable. The IList enumerator is handy for stepping through the elements of an ArrayList. All these specialized enumerators are derived from a base enumerator interface, IEnumerator. Because of this fact, all the collectio
| 对此文章发表了评论 |

