简体   繁体   English

如何在 Windows Forms 应用程序中创建 10 行结构数组

[英]how to create 10 row structure array in Windows Forms Application

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我被要求创建一个 10 行结构数组来显示、更新、读取和写入信息以更新 txt 文件,但是通过给我的这个示例代码,我不断收到一条错误消息,指出“错误 CS0246 类型或命名空间名称找不到“产品”(您是否缺少 using 指令或程序集参考?)”我对这些 arrays 迷路了

addtional info: this is a class level array, and this is a windows forms application附加信息:这是一个 class 级别数组,这是一个 windows forms 应用程序

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;

This is how you declare an array in C#. You also need to fill it up afterwards. C#就是这样声明一个数组的,之后还需要进行填充。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建第一个Windows Forms DevExpress应用程序 - How to create first Windows Forms DevExpress application 如何为Windows窗体应用程序创建安装程序和卸载程序 - How to create an installer and uninstaller for a Windows Forms application 调试 Windows 10 平板电脑的 Windows 窗体应用程序 - Debug Windows forms application for Windows 10 tablet 如何在Windows窗体应用程序中创建Alt快捷方式? - How can you create Alt shortcuts in a Windows Forms application? vs2010如何在Windows窗体应用程序中创建卸载过程? - how to create Uninstall process in the Windows forms application in vs2010? 如何在Windows窗体应用程序中检测Windows 10何时进入平板电脑模式? - How can I detect when Windows 10 enters tablet mode in a Windows Forms application? 如何在 Windows Forms (C#) 中创建带有图片框的数组 - How to create an array with pictureboxes in Windows Forms (C#) 如何发布Windows窗体应用程序 - How to publish a windows forms application Windows窗体应用程序将创建另一个可执行文件 - Windows Forms application that will create another executable file 如何在Windows 10的UWP应用和iOS设备的Xamarin.Forms应用之间创建TCP套接字 - how to create TCP socket between UWP app on Windows 10 and Xamarin.Forms app on iOS device
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM