user328146
user328146

Reputation:

Dynamic size two dimensional array

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?

Upvotes: 0

Views: 1084

Answers (3)

Dan Williams
Dan Williams

Reputation: 4950

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.

Upvotes: 1

Alex K.
Alex K.

Reputation: 175876

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").

Upvotes: 0

murgatroid99
murgatroid99

Reputation: 20297

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.

Upvotes: 0

Related Questions