开发者

Store data into Objects based on the input C#

开发者 https://www.devze.com 2023-01-25 20:44 出处:网络
I have to save data into different C# objects based on the input. Based on the value of \"productChoice\" my program should save data to the corresponding class.

I have to save data into different C# objects based on the input.

Based on the value of "productChoice" my program should save data to the corresponding class.

For example :

if productChoice = "auto" then the data shoud be set to AutoDataprefill object.

if productChoice = "vehicle" then the data shoud be set to VehicleMileage object.

My order class :

    public class Order
    {
       public Products  products {get;set;}
    }
 

My Products class :

  public class Products
    {
       public productChoiceType products { get; set; }
    }
  

My productChoiceType class :

   public class productChoiceType
    {
       public string productChoice { get; set; }
       public AutoDataprefill auto_dataprefill { get; set; }
       public VehicleMileage vehicle_mileage { get; set; }
       public ClaimsDiscovery claims_discovery { get; set; }
       public PropertyDataprefill property_dataprefill { get; set; }
       public List  productList { get; set; }
    }
   
 
 public class AutoDataprefill
    {
        public Parameter parameter { get; set; }
        public Pnc pnc { get; set; }
        public ProductReturnFormat format { get; set; }
        public string primary_subject { get; set; } // Attribute // IDREF
    }   
 
 
public class VehicleMileage
    {
       public string usage { get; set; }
       public VmrReportType report_type { get; set; }
       public Vehicles vehicles { get; set; }
       public string subject { get; set; } //Attribute // IDREF

    }
 

Here is my code to create an instance of "order" and save data to corresponding objects:

  CLUEAuto.Personal.BusinessEntities.Order nwOrder 开发者_如何转开发= new CLUEAuto.Personal.BusinessEntities.Order
       {


            products = new CLUEAuto.Personal.BusinessEntities.Products 
            {
               products = new CLUEAuto.Personal.BusinessEntities.productChoiceType 
                {
                    /* if (productChoice == "auto")
                           {Parameter = , Pnc = ,....   }      
                       elseif (productChoice == "vehicle")
                           {usage = , reportType = , ....}  */

                                   ???
                }
             }
        }   


Maybe it would be easier if you handle the creation of your objects this way:

string productChoice = "auto";
Order nwOrder = new Order();
Products nwProducts = new Products();
nwOrder.products = nwProducts;
productChoiceType nwPCT = new productChoiceType();
if(productChoice == "auto") {   
  AutoDataprefill adp = new AutoDataprefill();
  //adp.Parameter = ...
  //adp.Pnc = ...
  nwPCT.auto_dataprefill = adp;
}
else if (productChoice == "vehicle")
{
  // etc...
}
nwProducts.products = nwPCT;
0

精彩评论

暂无评论...
验证码 换一张
取 消