Simple variable class that imitates the shopping cart

Source: Internet
Author: User
After reading the shopping cart for nearly two hours this evening, I basically understood the principle. First, I will write a class similar to the structure for a simple demonstration.
The store class imitates the items in the shopping cart.
Public class store
{
Private string name;
Private int ID;
Private datetime time;

Public store (string name, int ID, datetime time)
{
This. Name = Name;
This. ID = ID;
This. Time = time;
}

// Attributes
Public string name
{
Get {return this. Name ;}
}

Public int ID
{
Get {return this. ID ;}
}

Public datetime time
{
Get {return this. Time ;}
}
}

Storelist class imitating shopping cart
Public class storelist: ienumerable
{
Arraylist Al = new arraylist ();
Public storelist ()
{}
// Add an item to the vehicle
Public void add (store st)
{
This. Al. Add (ST );
}

// Return all items
Public arraylist list
{
Get {return this. Al ;}
}
// Implement the ienumerable Interface
# Region ienumerable Member

Public ienumerator getenumerator ()
{
Return this. Al. getenumerator ();
}

# Endregion

// Add an index. Check whether the validity of the index quantity is determined.
Public store this [int Index]
{
Get {return (store) al [Index];}
}

// Number of items
Public int count
{
Get {return Al. Count ;}
}
}

Last demo page
Public class teststore: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. Button addstore;
Protected system. Web. UI. webcontrols. Label showmsg;

Private void page_load (Object sender, system. eventargs E)
{
Show ();
}

# Generated by region web Form DesignerCode
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. addstore. Click + = new system. eventhandler (this. addstore_click );
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion

// Click the Add button to add a product to the shopping cart saved in the session.
Private void addstore_click (Object sender, system. eventargs E)
{
Store ST = new store ("Alex", 0, datetime. Now );
// Check whether a shopping cart exists in the session. If not, add
If (session ["stxx"] = NULL)
{
Storelist SL = new storelist ();
Session ["stxx"] = SL;
}
// Get the shopping cart from the session and add a commodity to it
Storelist SLS = (storelist) session ["stxx"];
SLS. Add (ST );
// Pay attention to this and analyze this
// Session ["stxx"] = SLS;
}

// Display items in the shopping cart
Private void show ()
{
Stringbuilder sb = new stringbuilder ();
If (session ["stxx"]! = NULL)
{
Storelist SLS = (storelist) session ["stxx"];
// Use the index to retrieve items cyclically
For (INT I = 0; I <SLS. Count; I ++)
SB. append (SLS [I]. Time. tostring () + "<br> ");
Showmsg. Text = sb. tostring ();
}
}
}

store is a thin entity class, while storelist is a variable class. The storelist class saves the store class through the arraylist and provides corresponding methods to operate the store.
let's see this:
// get the shopping cart from the session and add a commodity to it.
storelist SLS = (storelist) session ["stxx"];
SLS. add (ST);
// pay attention to this, and finally analyze this
// session ["stxx"] = SLS;
This involves a session issue. Because our storelist is saved in the session, each operation must first retrieve the storelist from the session, however, after the operation, storelist is not saved back to the session. This is mainly because the extracted values are not saved in the session, however, we only get a reference to the value saved in the session, so subsequent operations are actually performed on the value saved in the session, so there is no need to save it at last.

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.