如何添加到结构数组中

时间:2021-02-18 15:59:47

I'm having troubles in adding into an array. I have created a class named Products:

我在添加数组时遇到了麻烦。我创建了一个名为Products的类:

public struct newProducts
{
    public string productBrand;
    public string productType;
    public string productName;
    public string productFlavour;
    public int productSize;
}

//Create an array of type newProducts
newProducts[] productList = new productList[];

and than I've created a function:

我创建了一个函数

public newProducts AddProduct(string brand, string type, string name, string flavour, int size)
{
    //I don't know what to do here..

    return productList;
}

What I want to do is to append and store the brand, type, name, flavour and size values to the array

我要做的是将品牌、类型、名称、风味和大小值添加并存储到数组中

Basically the first time I call this function I'll enter those values and store it to index 0 and on the second call it will add them to index 1 .

基本上,我第一次调用这个函数时,我将输入这些值并将其存储到索引0,在第二次调用时,它将它们添加到索引1。

Is this possible?

这是可能的吗?

2 个解决方案

#1


5  

You're better off using a List<T> instead of a T[]. That way, you can append values any time, without having to worry about resizing the array yourself

最好使用 而不是T[]列表。这样,您就可以随时附加值,而不必担心自己调整数组的大小

List<NewProduct> products = new List<NewProduct>();
products.Add(new Product { /* more code here */ });

Also, you might be coming from a lower level programming background, but as others mentioned, I'm not sure you really understand the true meaning of struct in C#. By looking at your code, you're looking for a class:

另外,您可能来自较低级别的编程背景,但正如其他人所提到的,我不确定您是否真正理解c#中struct的真正含义。通过查看代码,您正在寻找一个类:

public class NewProduct
{
    public string ProductBrand { get; set; }
    public string ProductType { get; set; }
    public string ProductName { get; set; }
    public string ProductFlavour { get; set; }
    public int ProductSize { get; set; }
}

I'd suggest starting by reading Classes and Structs (MSDN) and What's the difference between struct and class in .NET?

我建议从阅读class和struct (MSDN)开始,在。net中,struct和class有什么区别?

#2


0  

I've solved my issue, thank you for pointing out the differences between structures and classes, here's my approach:

我已经解决了我的问题,谢谢你指出结构和类之间的区别,这是我的方法:

I've changed the elements as Yuval described.. get and set methods..

我改变了Yuval描述的元素。获取和设置方法。

than created a list of type newProduct class in the main form class

在主窗体类中创建一个newProduct类类型的列表

later on inside the main form class I created a method to insert data to the class elements and added it to the list! and it worked fine

稍后,在main form类内部,我创建了一个方法,将数据插入到类元素中,并将其添加到列表中!它工作得很好

thanks everyone in advance and sorry for my bad english

提前谢谢大家,对我的英语很抱歉

#1


5  

You're better off using a List<T> instead of a T[]. That way, you can append values any time, without having to worry about resizing the array yourself

最好使用 而不是T[]列表。这样,您就可以随时附加值,而不必担心自己调整数组的大小

List<NewProduct> products = new List<NewProduct>();
products.Add(new Product { /* more code here */ });

Also, you might be coming from a lower level programming background, but as others mentioned, I'm not sure you really understand the true meaning of struct in C#. By looking at your code, you're looking for a class:

另外,您可能来自较低级别的编程背景,但正如其他人所提到的,我不确定您是否真正理解c#中struct的真正含义。通过查看代码,您正在寻找一个类:

public class NewProduct
{
    public string ProductBrand { get; set; }
    public string ProductType { get; set; }
    public string ProductName { get; set; }
    public string ProductFlavour { get; set; }
    public int ProductSize { get; set; }
}

I'd suggest starting by reading Classes and Structs (MSDN) and What's the difference between struct and class in .NET?

我建议从阅读class和struct (MSDN)开始,在。net中,struct和class有什么区别?

#2


0  

I've solved my issue, thank you for pointing out the differences between structures and classes, here's my approach:

我已经解决了我的问题,谢谢你指出结构和类之间的区别,这是我的方法:

I've changed the elements as Yuval described.. get and set methods..

我改变了Yuval描述的元素。获取和设置方法。

than created a list of type newProduct class in the main form class

在主窗体类中创建一个newProduct类类型的列表

later on inside the main form class I created a method to insert data to the class elements and added it to the list! and it worked fine

稍后,在main form类内部,我创建了一个方法,将数据插入到类元素中,并将其添加到列表中!它工作得很好

thanks everyone in advance and sorry for my bad english

提前谢谢大家,对我的英语很抱歉