views:

55

answers:

3

Hi,

I'm currently working on a webshop. For that i need to make a two dimensional array to store the items moved to the cart.

Cart:

Cart = Session("Cart")
Items = Session("Items")

And when an item is moved to the cart:

Items = Items + 1

Cart(1,Items) = Items
Cart(2,Items) = rs("id")
Cart(3,Items) = Request("attr")
Cart(4,Items) = rs("name")
Cart(5,Items) = rs("price")
Cart(6,Items) = 1

And finally:

Session("Cart") = Cart
Session("Items") = Items

But i'm having issues with the asp lack of proper support of dynamic sized two-dimensional arrays. Or am i just taking it the wrong way? Can you help me?

A: 

It seems to me that your problem could be solved with a dynamically sized list of item objects. In that case you would want to create an Item class and then add to the Cart list a new Item object for each new item.

murgatroid99
Do you have any links for documentation or code examples?
s0mmer
A: 

Would it not be simpler to store a ShoppingSessionID for the user that's related to a table that stores a list of items in the cart? that way all you have to store is Session("ShoppingSessionID").

Alex K.
This is my next solution.. I do prefer the one explained, but if there aren't decent support for multi-dimensional arrays ehn i'll go for this one.
s0mmer
+1  A: 

You might want to create some objects instead of using arrays. Or even a structure, if it's got no methods.

Here's an expamle of a struct

/// <summary>
/// Custom struct type, representing a rectangular shape
/// </summary>
struct Rectangle
{
    /// <summary>
    /// Backing Store for Width
    /// </summary>
    private int m_width;

    /// <summary>
    /// Width of rectangle
    /// </summary>
    public int Width 
    {
        get
        {
            return m_width;
        }
        set
        {
            m_width = value;
        }
    }

    /// <summary>
    /// Backing store for Height
    /// </summary>
    private int m_height;

    /// <summary>
    /// Height of rectangle
    /// </summary>
    public int Height
    {
        get
        {
            return m_height;
        }
        set
        {
            m_height = value;
        }
    }
}

so now you can:

Cart[0] = new Rectangle{Width = 1,Height = 3};

or

Rectangle myRec = new Rectangle();
myRec.Height = 3;
myRec.Width = 1;
Cart[0] = myRec;

Swap the Rectangle example with Item, and you should be on your way. That way, a single instance of each Cart multiple Items that each have their own set of properties.

Dan Williams
I would look into object. You have any code examples or links? I dont need more than one Cart per session - what makes you think that?
s0mmer
I added an example of a struct to my answer. http://www.blackwasp.co.uk/CSharpObjectOriented.aspx is a nice OO tutorial.
Dan Williams
I know structures.. But structures in ASP?..
s0mmer
Well, it's actually structures in C#, they're there for a reason :)
Dan Williams