Common asp.net Code techniques (DPC&DWC Reference)--5

Source: Internet
Author: User
Tags foreach empty error handling finally block reference tostring
asp.net Figure 2.4
Output of Listing 2.1.4 when viewed through a browser.

Adding Elements to a Queue
In Listing 2.1.4, we begin by creating a instance of the Queue class, Qtasks (line 5). In line 7 through, we add eight new elements to qtasks using the Enqueue method. Recall that a queue supports the "a" and "I", "I" Ordering T to is removed'll be "Wake up", which is the "the" the "the" "The" "the".

To quickly check if a particular element are an element of the queue, can be use the Contains method. Line demonstrates usage of the Contains method. Note This it takes a single parameter, the element to search for, and returns True if the element are found in the \ D False otherwise.

Removing Elements from a Queue
With a Queue, you can only remove the element in the head. With such a constraint, it's no wonder that the Queue class is only has a single member to remove a element:dequeue. Dequeue not only removes the element in the queue, but it also returns the element just removed.

If you are attempt to remove a element from empty Queue, the InvalidOperationException exception'll be thrown and your WI ll receive an error. Therefore, to prevent producing a runtime error in your ASP.net page, being sure to either place the Dequeue statement in a T Ry ... Catch... Finally block or ensure this Count property is greater than zero (0) before using Dequeue. (For more information on Try ...) Catch... Finally blocks, refer to Chapter 9, "asp.net Error handling." For a example of checking the Count property prior to using Dequeue, and then lines through in Listing.) As with the "All" collection types, you can remove all the "Queue elements with a" 36).

There might is times when you want to access the element at the "head of the" queue without removing it from the queue. This is possible via the ' Peek method ', which returns the element at the ' head ' of the Queue without removing it. As with the Dequeue method, if you are try to Peek a empty Queue, an InvalidOperationException exception'll be thrown.

Iterating through the Elements a Queue
One way to iterate through the elements of a \ to simply use dequeue to successively grab each item off the head. This approach can is seen in lines through Listing. The major disadvantage of this approach is and after iteration is complete, the \ is empty!

As with every other collection type, the Queue can is iterated via a for each ... Next loop or through the use of a enumerator. The following code snippet illustrates using the C # foreach statement to iterate all through of a Queue elements UT affecting the structure:

Queue Qmyqueue = new Queue (); Create a Queue

Qmyqueue.enqueue (5);
Qmyqueue.enqueue (62); Add some elements to the Queue
Qmyqueue.enqueue (-7);

Iterate through each element of the Queue, displaying it
foreach (int i in qmyqueue)
Response.Write ("Visiting Queue Element with Value:" + i + "<br>");
Working with the Stack Class
A stack is a data structure similar to a \ It supports only sequential access. However, a stack does bear one major difference to queue:rather than storing with a O) semantic, a stack uses last in, and (LIFO). A crowded elevator behaves similar to a stack:the of the WHO enters the crowded elevator are the last person to Leav E, whereas the board the elevator is the the ' the ' the ' the ' it reaches its destination.

adding, removing, and accessing Elements in a Stack
The. NET Framework provides an implementation of the stack data type with the Stack class. A stack has two basic operations:adding a element to the top of the stack, which are accomplished with the Push method, a nd removing an element from the top of the stack, accomplished via the Pop method. Similar to the Queue class, the Stack class also contains a Peek method to permit developers to access the top of the Stac K without removing the element.

Up until this point, the code provided in the previous listings has just given your a feel for the syntax of the various CO Llections. Listing 2.1.5, however, contains a handy little piece of reusable code that can is placed on each page of the your Web site to Provide a set of navigation history links for your visitors.

The code in Listing 2.1.5 uses a session-level Stack class instance, which used to store the links that a Web visitor has Traversed on your site since the "start" session. Each time a user visits a Web page, the stack are displayed in a history label and the page's URL is pushed onto the stack. As the user visits various pages on your Web site, his navigation history stack would continue to grow and he would be able To quickly jump previous pages on your site. Basically, this is mimicking the functionality of a browser ' s Back button. The output is shown in Figure 2.5.

Listing 2.1.5 A Stack is Ideal for keeping Track of a User ' s navigation History
1: <script language= "C #" runat= "Server" >
2:
3:void Page_Load (Object sender, EventArgs e)
4: {
5://If we have a stack created or not:
6:if (session["History"] = = null)
7: {
8://The history stack has not been created, so create it now.
9:session["History"] = new Stack ();
} else {
One://We already have a history stack. Display The history:
12:ienumerator EnumHistory =
((Stack) session["History"]). GetEnumerator ();
14:while (Enumhistory.movenext ())
15:lblstackhistory.text + = "<a href=\" "+ enumhistory.current +
: ">" + enumhistory.current +
: "</a><br>";
18:}
19:
://Push current URL onto Stack IF It isn't already on the top
21:if ((Stack) session["History"). Count > 0)
22: {
23:if ((Stack) session["History"). Peek (). ToString ()!=
24:request.url.pathandquery.tostring ())
((Stack) session["History"]). Push (Request.Url.PathAndQuery);
:} else
((Stack) session["History"]). Push (Request.Url.PathAndQuery);
28:}
29:
</script>
31:
: <body>
: <b>session history</b><br>
: <asp:label runat=server id= "Lblstackhistory"/><br>
36:
Notoginseng: <a href= "ClearStackHistory.CSharp.aspx" >clear Stack history</a><br>
<a href= "Back.CSharp.aspx" >Back</a>
39:
<p>
: <b>Links:</b><br>
: <li><a href= "listing2.1.5.aspx" >Listing2.1.5.aspx</a><br>
<li><a href= "Listing2.1.5.b.aspx" >Listing2.1.5.b.aspx</a><br>
: </body>
:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.