Home > Blockchain >  how to create 10 row structure array in Windows Forms Application
how to create 10 row structure array in Windows Forms Application

Time:04-14

Product[] inventoryProducts = new Product[100];

I was asked to create a 10 row structure array for displaying, updating, reading, and writing info to update a txt file, but with this example code given to me, I keep receiving an error that says "Error CS0246 The type or namespace name 'Product' could not be found (are you missing a using directive or an assembly reference?)" I am so lost with these arrays

addtional info: this is a class level array, and this is a windows forms application

Figured it out in case anyone wants to know...

        struct Product
    {
        public string ProdId;
        public string ProdName;
        public string ProdDesc;
        public int ProdQuantityOnHand;
        public int ProdReorderQuantity;
        public decimal ProdPrice;
    }

    //create a 10 row array of Products:
    Product[] allProducts = new Product[10];
    int arrayIndex = 0;

CodePudding user response:

If you hover above the Error they give you possible solution. Could you maybe just add the directory? Or is the Class set private?

CodePudding user response:

This is how you declare an array in C#. You also need to fill it up afterwards.

/// <summary>
/// Defines a product
/// </summary>
public class Product
{
    public Product(string name, string vendor, decimal price)
    {
        Name=name;
        Vendor=vendor;
        Price=price;
    }

    public string Name { get; }
    public string Vendor { get; set; }
    public decimal Price { get; set; }
}

static class Program
{
    static void Main(string[] args)
    {
        Product[] array = new Product[10];
        // array of 10 items (rows) is declared, but it is empty:
        // array[0] = null;
        // array[1] = null;
        // ..

        // Fill the array
        array[0] = new Product("PIN", "A", 0.06m);
        array[1] = new Product("PEN", "A", 0.86m);
        array[2] = new Product("PAN", "B", 8.12m);
        // ..
        // etc
    }
}
  • Related