如何定义来自不同类的多维数组
Posted
技术标签:
【中文标题】如何定义来自不同类的多维数组【英文标题】:How to define a multidimension array from different classes 【发布时间】:2021-07-02 19:31:29 【问题描述】:我正在尝试使用来自第二个类的值和多维数组创建对象。我创造了什么:
class ProductData
private ProductPrices[] prices;
public string ParentName get; set;
public string Name get; set;
public string Description get; set;
public string TechnicalDescription get; set;
public bool Obsolete get; set;
public bool Deleted get; set;
public string Innercarton get; set;
public string Outercarton get; set;
public string Package get; set;
public string Barcode get; set;
public decimal Weight get; set;
public decimal EndUserPrice get; set;
public int MOQ get; set;
public bool ComingSoon get; set;
public bool NewProduct get; set;
public string Equivalent get; set;
public string ReplacedBy get; set;
public ProductPrices[] Prices
get
return prices;
set
prices = value;
public List<ProductFeatures> Feature get; set;
public List<string> Specification get; set;
public List<string> Image get; set;
public string File get; set;
public string Icon get; set;
class ProductFeatures
public string Feature;
public class ProductPrices
public decimal Price;
public int MinQuantity;
public DateTime validuntil;
public string currency;
现在我想为 ProductPrices 设置一个多维数组。 所以,最后我会有这样的东西:
Dictionary<string, ProductData> productInfo = new Dictionary<string, ProductData>();
//Adding data to productInfo
string productName = productInfo.Name;
string description = productInfo.Description;
decimal wholesalePrice = productInfo.ProductPrices[0].Price;
int minQty = productInfo.ProductPrices[0].MinQuantity;
某处我错过了一些东西。我一直在寻找几个小时试图找到它,但不幸的是...... 提前致谢!
【问题讨论】:
std::vector<ProductPrices> Prices
是你想要的吗?
我在您的代码中没有看到需要二维气道的任何地方。看起来你只是不知道如何使用字典。你能解释一下你的问题吗?
【参考方案1】:
我已经找到了。
删除private ProductPrices[] prices;
并更改
public ProductPrices[] Prices
get
return prices;
set
prices = value;
到
public ProductPrices[] Prices get; set;
之后,你可以这样做:
ProductData prData = new ProductData();
prData.ParentName = "Parent name";
prData.Name = "Some name";
prData.Prices = new ProductPrices[enterArrayCount];
ProductPrices prdPrices = new ProductPrices();
prdPrices.Price = Convert.ToDecimal(yourDecimalValue);
prdPrices.MinQuantity = 8;
prdPrices.Currency = "Eur";
prData.Prices[firstIndex] = prdPrices; //So firstIndex must increase after a value has been set.
【讨论】:
以上是关于如何定义来自不同类的多维数组的主要内容,如果未能解决你的问题,请参考以下文章