how iterate over Session objeckts? (1 Viewer)

pikej

New member
Local time
Today, 09:42
Joined
Jan 20, 2010
Messages
2
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
 

wazz

Super Moderator
Local time
Tomorrow, 00:42
Joined
Jun 29, 2004
Messages
1,711
Items is public. can you get to it by adding:

using ShoppingCart;

to Cart.aspx.cs?

Edit: it's protected. well, not sure now unless you "unprotect" it.
// A protected constructor ensures that an object can't be created from outside
protected ShoppingCart() { }

or perhaps store session variables in a database or other way.
 
Last edited:

pikej

New member
Local time
Today, 09:42
Joined
Jan 20, 2010
Messages
2
[FONT=Arial, Helvetica, Geneva, SunSans-Regular, sans-serif]thanks for your answer,

I made it public and added using ShoppingCart; but now I get: Compiler Error Message:
CS0138: A using namespace directive can only be applied to namespaces; 'ShoppingCart' is a type not a namespace
[/FONT]
 
Last edited:

Users who are viewing this thread

Top Bottom