SHARE
TWEET

Untitled

a guest Jun 15th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CarConfiguration carConfiguration = new CarConfiguration()
  2. {
  3.     CarName = "Ferrari",
  4.     CarModel = "LaFerrari",
  5.     Spolier = true,
  6.     BuildDate = new DateTime(2018, 01, 01)
  7. };
  8.  
  9. //Thats not work because the "conf" parameter is never assign in the Car constructor
  10. Car myOwnCar = new Car(conf =>
  11. {
  12.     conf = carConfiguration;
  13. });
  14. Console.WriteLine(myOwnCar.CarConfigurationText());
  15.  
  16. //That works, but is not my purpose do it like this !
  17. Car myOtherCar = new Car(carConfiguration);
  18. Console.WriteLine(myOtherCar.CarConfigurationText());
  19.      
  20. public class CarConfiguration
  21. {
  22.     public bool Spolier { get; set; } = false;
  23.     public string CarName { get; set; } = String.Empty;
  24.     public string CarModel { get; set; } = String.Empty;
  25.     public DateTime BuildDate { get; set; } = default(DateTime);
  26. }
  27.      
  28. public class Car
  29. {
  30.     private CarConfiguration carConfiguration = null;
  31.  
  32.     //Thats not work because carConfiguration is not assigned in the Action as a reference
  33.     public Car(Action<CarConfiguration> configureCar)
  34.     {
  35.         configureCar(carConfiguration);
  36.     }
  37.  
  38.     //Thats works!
  39.     public Car(CarConfiguration configureCar)
  40.     {
  41.         carConfiguration = configureCar;
  42.     }
  43.  
  44.     public string CarConfigurationText()
  45.     {
  46.         StringBuilder strBuilder = new StringBuilder();
  47.  
  48.         if (carConfiguration != null)
  49.         {
  50.             strBuilder.AppendLine(carConfiguration.CarModel);
  51.             strBuilder.AppendLine(carConfiguration.CarName);
  52.             strBuilder.AppendLine(carConfiguration.Spolier.ToString());
  53.             strBuilder.AppendLine(carConfiguration.BuildDate.ToString("mm-DD-yyyy"));
  54.         }
  55.         else
  56.         {
  57.             strBuilder.AppendLine("Car is not configure!");
  58.         }
  59.  
  60.         return strBuilder.ToString();
  61.     }
  62. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top