Note that there are some explanatory texts on larger screens.

plurals
  1. POIs 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?
    primarykey
    data
    text
    <p>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.</p> <p>Here is the code:</p> <pre><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; } } </code></pre> <p>And then to instantiate the array:</p> <pre><code>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}; </code></pre> <p>Having overridden the <code>ToString()</code> method, I loop through it like so:</p> <pre><code>ProductStatus status = ProductStatus.ForSale; ProductStatus nextStatus = status + 1; for (int i = 0; i &lt; products.Length; i++) { Product p = products[i]; Console.WriteLine("{0}: " + p.ToString(), status.ToString()); } </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload