private static void NotAllowedCars()
        {
            try
            {
                // Get the current configuration file.
                System.Configuration.Configuration config =
                    ConfigurationManager.OpenExeConfiguration(
                        ConfigurationUserLevel.None);

                Automobile dreamCar = new Automobile();
                dreamCar.Color = "Red";
                dreamCar.Make  = "BMW";
                dreamCar.Miles = 10;
                dreamCar.Year  = 2005;

                Automobile commuteCar = new Automobile();
                commuteCar.Color = "Blue";
                commuteCar.Make  = "Yugo";
                commuteCar.Miles = 10;
                commuteCar.Year  = 1990;

                // Get the Cars section.
                SelectCar cars =
                    config.GetSection("Cars") as SelectCar;

                cars.Dream   = dreamCar;
                cars.Commute = commuteCar;
            }
            catch (ConfigurationErrorsException err)
            {
                Console.WriteLine(err.ToString());
            }
        }
Esempio n. 2
0
        public override object ConvertFrom(
            ITypeDescriptorContext ctx, CultureInfo ci, object data)
        {
            Automobile selectedCar =
                new Automobile();

            string carInfo = (string)data;

            string[] carSpecs = carInfo.Split(new Char[] { ' ' });

            // selectedCar.Make = carSpecs[0].ToString();
            // selectedCar.Make = carSpecs[0].ToString();

            string make =
                carSpecs[(int)Automobile.specification.make].ToString();
            string color =
                carSpecs[(int)Automobile.specification.color].ToString();
            string miles =
                carSpecs[(int)Automobile.specification.miles].ToString();
            string year =
                carSpecs[(int)Automobile.specification.year].ToString();


            selectedCar.Make =
                make.Substring(make.IndexOf(":") + 1);
            selectedCar.Color =
                color.Substring(color.IndexOf(":") + 1);
            selectedCar.Miles =
                Convert.ToInt32(miles.Substring(miles.IndexOf(":") + 1));
            selectedCar.Year =
                Convert.ToInt32(year.Substring(year.IndexOf(":") + 1));

            return(selectedCar);
        }
        public override void Validate(object value)
        {
            ArrayList make = new ArrayList();

            make.Add("Ferrari");
            make.Add("Porsche");
            make.Add("Lamborghini");

            int    minYear  = 2004;
            long   maxMiles = 100;
            string color    = "red";

            Automobile car = (Automobile)value;


            try
            {
                if (!make.Contains(car.Make))
                {
                    throw new ConfigurationErrorsException(
                              "My dream car is not made by " + car.Make);
                }

                // Make sure the year is valid
                if (car.Year < minYear)
                {
                    throw new ConfigurationErrorsException(
                              "My dream car cannot be older than " + minYear.ToString());
                }

                // Make sure the car can still run on its own
                if (car.Miles > maxMiles)
                {
                    throw new ConfigurationErrorsException(
                              "My dream car drive odometer cannot read more than " +
                              maxMiles.ToString() + " miles");
                }

                // Validate color
                if (car.Color.ToLower() != color)
                {
                    throw new ConfigurationErrorsException(
                              "My dream car can oly be " + color);
                }
            }
            catch (ConfigurationErrorsException err)
            {
                Console.WriteLine(err.ToString());
            }
        }
        public override void Validate(object value)
        {
            Automobile car = (Automobile)value;

            try
            {
                // Validate make
                if (car.Make != pmake)
                {
                    throw new ConfigurationErrorsException(
                              "I do not by cars made by " + car.Make);
                }

                // Validate color
                if (car.Color != pcolor)
                {
                    throw new ConfigurationErrorsException(
                              "My commute car must be " + pcolor);
                }

                // Validate year
                if (car.Year < pminYear)
                {
                    throw new ConfigurationErrorsException(
                              "It's about time you get a new car.");
                }

                // Validate miles
                if (car.Miles > pmaxMiles)
                {
                    throw new ConfigurationErrorsException(
                              "Don't take too long trips with that car.");
                }
            }
            catch (ConfigurationErrorsException err)
            {
                Console.WriteLine(err.ToString());
            }
        }