类数组定义

时间:2013-10-09 09:22:47

标签: c# visual-studio class

我似乎无法弄清楚如何填充子类......

这些是我的课程:

public class Item
{
    public Meta meta { get; set; }
    public Items[] objects { get; set; }
}

public class Meta
{
    public int limit { get; set; }
    public object next { get; set; }
    public int offset { get; set; }
    public object previous { get; set; }
    public int total_count { get; set; }
}

public class Items
{
    public Additionalprices additionalPrices { get; set; }
    public string barcode { get; set; }
    public Category category { get; set; }
    public int categoryPosition { get; set; }
    public string cdate { get; set; }
    public DateTime ctime { get; set; }
    public DateTime ctimeOnServer { get; set; }
    public Dimensions dimensions { get; set; }
    public string entityType { get; set; }
    public object imageURL { get; set; }
    public object[] imageURLs { get; set; }
    public string longDesc { get; set; }
    public string manufacturer { get; set; }
    public int minQty { get; set; }
    public DateTime mtime { get; set; }
    public DateTime mtimeOnServer { get; set; }
    public int multQty { get; set; }
    public string name { get; set; }
    public int objID { get; set; }
    public string owner { get; set; }
    public string resource_uri { get; set; }
    public string sku { get; set; }
    public object thumbnailURL { get; set; }
    public object[] thumbnailURLs { get; set; }
    public string unitPrice { get; set; }
    public string uuid { get; set; }
    public Variant[] variants { get; set; }
}

我想添加一些虚拟对象......

这是我的代码......

// Create a dummy Category
HandShake_Classes.Category categoryToUpload = new HandShake_Classes.Category();

categoryToUpload.name = "Trev Category";

// Create a dummy Item
HandShake_Classes.Item itemToUpload = new HandShake_Classes.Item();

// THIS IS WHERE MY PROBLEM IS...
itemToUpload.objects = new HandShake_Classes.Items();
// THE ERROR I AM GETTING IS Error  2   Cannot implicitly convert type 'handshake.com.HandShake_Classes.Items' to 'handshake.com.HandShake_Classes.Items[]'

// Only populate required fields at the moment
itemToUpload.objects[0].sku = "ljklj";
itemToUpload.objects[0].name = "Trevs Product";
itemToUpload.objects[0].unitPrice = "1.23";
itemToUpload.objects[0].minQty = 1;
itemToUpload.objects[0].category = categoryToUpload;

我无法弄清楚如何定义Items []我认为

有人能指出我正确的方向吗?

由于

2 个答案:

答案 0 :(得分:2)

您必须先初始化阵列。假设您需要10件物品:

itemToUpload.objects = new HandShake_Classes.Items[10];

然后您可以将每个数组项设置为新的Items()实例:

itemToUpload.objects[0] = new HandShake_Classes.Items();
itemToUpload.objects[0].sku = "ljklj";
itemToUpload.objects[0].name = "Trevs Product";
itemToUpload.objects[0].unitPrice = "1.23";
itemToUpload.objects[0].minQty = 1;
itemToUpload.objects[0].category = categoryToUpload;

并且要说清楚:如果您不知道object属性必须处理多少项,您应该考虑将其更改为List<Items>而不是Items[]数组:

itemToUpload.objects = new List<HandShake_Classes.Items>();

var newItem = new HandShake_Classes.Items();
newItem = new HandShake_Classes.Items();
newItem.sku = "ljklj";
newItem.name = "Trevs Product";
newItem.unitPrice = "1.23";
newItem.minQty = 1;
newItem.category = categoryToUpload;

itemToUpload.objects.Add(newItem);

答案 1 :(得分:1)

将类型项数组实例化为

itemToUpload.objects = new HandShake_Classes.Items[10];

在此实例化之后,您需要实例化对象的每个索引。

例如

itemToUpload.objects[i] = new HandShake_Classes.Items();

这意味着您要实例化一个对象,该对象将位于索引“i”的数组中。

然后,您可以通过写为

来设置对象数组中Items的属性

itemToUpload.objects[i].barcode

甚至调用函数

itemToUpload.objects[i].CalculateTax()

同样,如果已定义,您可以使用任何构造函数。