[英]how to create 10 row structure array in Windows Forms Application
Product[] inventoryProducts = new Product[100];
我被要求创建一个 10 行结构数组来显示、更新、读取和写入信息以更新 txt 文件,但是通过给我的这个示例代码,我不断收到一条错误消息,指出“错误 CS0246 类型或命名空间名称找不到“产品”(您是否缺少 using 指令或程序集参考?)”我对这些 arrays 迷路了
附加信息:这是一个 class 级别数组,这是一个 windows forms 应用程序
弄清楚以防万一有人想知道...
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;
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.