开发者

Is there a way to loop through an array, edit the contents of an array in C#, and the loop through it again to show the edited contents?

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 f开发者_运维问答rom "ForSale" to "Sold" (and then "Shipped" afterwards), and then loop through the array again to show the changes. How does one do that?


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.


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


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);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜