0

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 = , ....}  */

                                   ???
                }
             }
        }   
jdecuyper
  • 3,934
  • 9
  • 39
  • 51
BumbleBee
  • 10,429
  • 20
  • 78
  • 123
  • how should my code look like for this psuedo code : /* if (productChoice == "auto") {Parameter = , Pnc = ,.... } elseif (productChoice == "vehicle") {usage = , reportType = , ....} */ – BumbleBee Nov 17 '10 at 23:16

1 Answers1

1

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;
jdecuyper
  • 3,934
  • 9
  • 39
  • 51
  • Thank you. Even though I implied "public" to the members, but it seems the Compiler makes the member as private. When I put . after nwOrder the intellisense doesn't show anything to me. – BumbleBee Nov 17 '10 at 23:36
  • That strange because I used your classes in a small web project and intellisense worked as expected for the Order class. Maybe it has something to do with the IDE you are using. – jdecuyper Nov 17 '10 at 23:55
  • It is really annoying. The runtime automatically sets them to private. I don't have any idea how to overcome this problem. – BumbleBee Nov 18 '10 at 00:00
  • That sounds like a predefined setting from your IDE. Which one are you using? – jdecuyper Nov 18 '10 at 00:24