Trevor Daniel
Trevor Daniel

Reputation: 3974

Class Array Defining

I cannot seem to work out how to populate a sub class...

These are the classes I have:

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

I want to add some dummy objects...

This is the code I have...

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

I just cannot work out how define the Items[] I think

Can anyone point me in the right direction please?

Thanks

Upvotes: 0

Views: 77

Answers (2)

Tauseef
Tauseef

Reputation: 2052

Instantiate the array of type items as

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

After this instantiation, you would require to instantiate every index of the object.

For example

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

This means you are instantiating a single object, and that object would be in array at index 'i'.

you can then set the properties of the Items in objects array by writing as

itemToUpload.objects[i].barcode

even call the functions

itemToUpload.objects[i].CalculateTax()

similarly you can use any constructors if defined.

Upvotes: 1

MarcinJuraszek
MarcinJuraszek

Reputation: 125650

You have to initialize the array first. Let's say you need 10 items:

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

And then you can set every array item to the new Items() instance:

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;

And just to make it clear: if you don't know how many items object property will have to handle, you should consider changing it to List<Items> instead of Items[] array:

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);

Upvotes: 2

Related Questions