有没有办法循环遍历数组,在 C# 中编辑数组的内容,然后再次循环以显示编辑后的内容?

发布于 2024-12-06 09:21:17 字数 1293 浏览 1 评论 0原文

我正在尝试遍历一系列产品。我想知道是否可以更改数组的元素,然后再次循环以将其与编辑后的元素一起显示。

下面是代码:

public enum ProductStatus
{
    ForSale,
    Sold,
    Shipped
}

class Product
{
    private string description;
    private decimal price;
    private ProductStatus status;

    public string Description
    {
        get { return description; }
        set { description = value; }
    }

    public decimal Price
    {
        get { return price; }
        set { price = value; }
    }

    public ProductStatus Status
    {
        get { return status; }
        set { status = 0; }
    }

然后实例化数组:

Product[] products = new Product[3];
products[0] = new Product() { Description = "Dress", Price = 69.99M };
products[1] = new Product() { Description = "Hat", Price = 5.95M};
products[2] = new Product() { Description = "Slacks", Price = 32.00M};

重写了 ToString() 方法后,我像这样循环遍历它:

ProductStatus status = ProductStatus.ForSale;
ProductStatus nextStatus = status + 1;

for (int i = 0; i < products.Length; i++)
{
    Product p = products[i];
    Console.WriteLine("{0}: " + p.ToString(), status.ToString());
}

我想使用 nextStatus 将第一个索引的 ProductStatus 从 " 更改ForSale”到“Sold”(然后是“Shipped”),然后再次循环数组以显示更改。如何做到这一点?

I am trying to loop through an array of products. I was wondering if it would at all be possible to change an element of the array, and then loop through it again to display it with the edited element.

Here is the code:

public enum ProductStatus
{
    ForSale,
    Sold,
    Shipped
}

class Product
{
    private string description;
    private decimal price;
    private ProductStatus status;

    public string Description
    {
        get { return description; }
        set { description = value; }
    }

    public decimal Price
    {
        get { return price; }
        set { price = value; }
    }

    public ProductStatus Status
    {
        get { return status; }
        set { status = 0; }
    }

And then to instantiate the array:

Product[] products = new Product[3];
products[0] = new Product() { Description = "Dress", Price = 69.99M };
products[1] = new Product() { Description = "Hat", Price = 5.95M};
products[2] = new Product() { Description = "Slacks", Price = 32.00M};

Having overridden the ToString() method, I loop through it like so:

ProductStatus status = ProductStatus.ForSale;
ProductStatus nextStatus = status + 1;

for (int i = 0; i < products.Length; i++)
{
    Product p = products[i];
    Console.WriteLine("{0}: " + p.ToString(), status.ToString());
}

I'd like to use the nextStatus to change the first index's ProductStatus from "ForSale" to "Sold" (and then "Shipped" afterwards), and then loop through the array again to show the changes. How does one do that?

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

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

发布评论

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

评论(3

一绘本一梦想 2024-12-13 09:21:17

虽然您的问题有点不清楚,但我想这就是您想要的(下面的代码测试)。

private void ChangeStatus(ProductStatus initialStatus, Product[] products)
{
    ProductStatus nextStatus = initialStatus + 1;
    foreach (var p in products)
    {
        p.Status = nextStatus;
    }
}

private void ShowProducts(Product[] products)
{
    foreach (var p in products)
    {
        Console.WriteLine("{0}: " + p.ToString(), status.ToString());
    }
}

输出可能如下所示(假设 ForSale 为初始状态):

P1(之前): 待售 - P1(之后)已售出
P2(之前): 已售出 - P2(之后): 已售出

无论如何,您确定要申请吗这样做?在我看来,您想要实现的目标略有不同,如下所示:

private void ChangeStatus(Product[] products)
{
    foreach (var p in products)
    {
        p.Status = p.Status + 1;
    }
}

在这种情况下,输出如下:

P1(之前): 待售 - P1(之后)已售出
P2(之前): 已售出 - P2(之后): 已发货

希望有帮助。

Although your question is a little unclear I suppose this is what you want (the code below is not tested).

private void ChangeStatus(ProductStatus initialStatus, Product[] products)
{
    ProductStatus nextStatus = initialStatus + 1;
    foreach (var p in products)
    {
        p.Status = nextStatus;
    }
}

private void ShowProducts(Product[] products)
{
    foreach (var p in products)
    {
        Console.WriteLine("{0}: " + p.ToString(), status.ToString());
    }
}

And the output could appear as follows (assuming ForSale as initial status):

P1 (before): ForSale - P1 (after): Sold
P2 (before): Sold - P2 (after): Sold

Anyway, are you sure you want your application to behave this way? In my opinion what you are trying to achieve is slightly different, something like this:

private void ChangeStatus(Product[] products)
{
    foreach (var p in products)
    {
        p.Status = p.Status + 1;
    }
}

In this case the output would be the following:

P1 (before): ForSale - P1 (after): Sold
P2 (before): Sold - P2 (after): Shipped

Hope it helps.

旧夏天 2024-12-13 09:21:17

如果您的问题是“如何确定更改的项目并查看它们”,您可以轻松使用其结尾列表:

private List<int> indexes = new List<int>();

Loop through items
{
 if (item is changed)
  indexes.Add(item's index);
}

If your question is "How can I determine the changed items and view them", you can easily use a list of their endexes:

private List<int> indexes = new List<int>();

Loop through items
{
 if (item is changed)
  indexes.Add(item's index);
}
是你 2024-12-13 09:21:17

为什么不创建一个包含更改状态的新集合,例如:

    ProductStatus status = ProductStatus.ForSale;
    ProductStatus nextStatus = status + 1;
    var statusChangesList = new List<KeyValuePair<Product, Status>>();
    foreach (var product in products)
    {
        statusChangesList.Add(new KeyValuePair<Product, Status>(product, product.Status));
        product.Status = nextStatus;
    }

    foreach (var statusChange in statusChangesList)
    {
        Console.WriteLine("Product: " + statusChange.key + " changed status from: " + statusCahnge.value);
    }

Why not create a new collection containing the changed statuses, e.g.:

    ProductStatus status = ProductStatus.ForSale;
    ProductStatus nextStatus = status + 1;
    var statusChangesList = new List<KeyValuePair<Product, Status>>();
    foreach (var product in products)
    {
        statusChangesList.Add(new KeyValuePair<Product, Status>(product, product.Status));
        product.Status = nextStatus;
    }

    foreach (var statusChange in statusChangesList)
    {
        Console.WriteLine("Product: " + statusChange.key + " changed status from: " + statusCahnge.value);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文