Reputation: 433
I have a class, BaseRequest that has another class as one of its properties. That class has its own set of properties, some of which are also classes, and some of those classes properties are classes. How to I initialize the BaseRequest for ProductOptions-GroupsToRun & SelectionTargets?
BaseRequest.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Redundent.Models
{
public class BaseRequest
{
public ProductOptions ProductOptions { get; set; }
public string RequestingSystem { get; set; }
public bool HasDeposit { get; set; }
public decimal? PurchasePrice { get; set; }
public decimal? PaymentAmount { get; set; }
}
}
ProductOptions.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Redundent.Models
{
public class ProductOptions
{
public GroupOption[] GroupsToRun { get; set; }
public string Requestor { get; set; }
public SelectionTargets SelectionTargets { get; set; }
public string Partner { get; set; }
}
}
GroupOption.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Redundent.Models
{
public class GroupOption
{
public TransportationType TransportationType { get; set; }
public RegistrationType Type { get; set; }
}
}
TransportationType and RegistrationType are enums. TransportationType has Car, Airplane, Boat, Bus and RegistrationType has Ticket, ID, Passport
SelectionTargets.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Redundent.Models
{
public class SelectionTargets
{
public PricingOptionTargets PricingOptionTargets { get; set; }
public ProductTargets ProductTargets { get; set; }
}
}
This is what I have started...
var baseRequest = new BaseRequest()
{
ProductOptions = { GroupsToRun = {//not sure what to do here. Tried TransportationType.Car but got "does not contain a definition for 'Add' and no accessible extension method 'Add'}, Requestor = "Ford", SelectionTarget = {also not sure what to do here}, Partner =
"Heuwett B." },
};
Upvotes: 0
Views: 846
Reputation: 5802
The GroupsToRun
field is an array, so an array initialiser has to be used to create an array. Checkout the example below:
var baseRequest = new BaseRequest()
{
ProductOptions = new ProductOptions
{
GroupsToRun = new[]
{
new GroupOption
{
TransportationType = new TransportationType {}
}
},
Requestor = "Ford",
SelectionTargets = new SelectionTargets
{
PricingOptionTargets = new PricingOptionTargets(),
ProductTargets = new ProductTargets()
},
Partner = "Heuwett B."
},
};
Upvotes: 2