Hello guys,
I have following questions. I am trying to write a simple e-commerce site and I use some code of my own. Basically it is a shopping cart. It uses Session to store some data, here comes some code:
/**
* The ShoppingCart class
*
* Holds the items that are in the cart and provides methods for their manipulation
*/
public class ShoppingCart {
#region Properties
public List<CartItem> Items { get; private set; }
#endregion
#region Singleton Implementation
// Readonly properties can only be set in initialization or in a constructor
public static readonly ShoppingCart Instance;
// The static constructor is called as soon as the class is loaded into memory
static ShoppingCart() {
// If the cart is not in the session, create one and put it there
// Otherwise, get it from the session
if (HttpContext.Current.Session["ASPNETShoppingCart"] == null) {
Instance = new ShoppingCart();
Instance.Items = new List<CartItem>();
HttpContext.Current.Session["ASPNETShoppingCart"] = Instance;
} else {
Instance = (ShoppingCart)HttpContext.Current.Session["ASPNETShoppingCart"];
}
}
// A protected constructor ensures that an object can't be created from outside
protected ShoppingCart() { }
#endregion
#region Item Modification Methods
/**
* AddItem() - Adds an item to the shopping
*/
public void AddItem(int productId) {
// Create a new item to add to the cart
CartItem newItem = new CartItem(productId);
// If this item already exists in our list of items, increase the quantity
// Otherwise, add the new item to the list
if (Items.Contains(newItem)) {
foreach (CartItem item in Items) {
if (item.Equals(newItem)) {
item.Quantity++;
return;
}
}
} else {
newItem.Quantity = 1;
Items.Add(newItem);
}
}
Now, I need to get some values from variables from this Session to send them to paypal. I can write some loop in Shoppinc cart to iterate over these objects:
foreach (CartItem item in Items)
The problem is that I need similar loop on a different page, Cart.aspx.cs, and foreach (CartItem item in Items) doesn't work any more.
How can I access these variables from Cart.aspx.cs?
Sorry for all this code. I would really appreciate some help.
All the best,
Pikej
!--removed tag-->
Enter your message below
Sign in or Join us (it's free).