动态大小二维数组

发布于 2024-09-12 03:28:32 字数 502 浏览 3 评论 0原文

我目前正在一家网上商店工作。为此,我需要制作一个二维数组来存储移到购物车的物品。

购物车:

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

当一个项目被移动到购物车时:

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

最后:

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

但是我遇到了 asp 缺乏对动态大小的二维数组的适当支持的问题。或者我只是采取了错误的方式?你能帮助我吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神仙妹妹 2024-09-19 03:28:32

您可能想创建一些对象而不是使用数组。或者甚至是 结构(如果有)没有方法。

这是一个结构体的示例

/// <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;
        }
    }
}

,现在您可以:

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

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

将 Rectangle 示例与 Item 交换,您应该就可以了。
这样,每个购物车的单个实例就有多个项目,每个项目都有自己的一组属性。

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.

沫尐诺 2024-09-19 03:28:32

为与存储购物车中的商品列表的表相关的用户存储 ShoppingSessionID 不是更简单吗?这样,您只需存储 Session("ShoppingSessionID")

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

怪我太投入 2024-09-19 03:28:32

在我看来,您的问题可以通过动态调整大小的项目对象列表来解决。在这种情况下,您需要创建一个 Item 类,然后将每个新项目的新 Item 对象添加到购物车列表中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文