示例#1
0
        public static bool CheckProperty(Garage i_Garage, string i_Value, string i_Field, string i_Type)
        {
            bool         boolField        = false;
            string       stringField      = "";
            float        floatField       = 0;
            int          intField         = 0;
            eCarDoors    doorField        = eCarDoors.Five;
            eColor       colorField       = eColor.Black;
            eLicenseType licenseTypeField = eLicenseType.A;
            string       colorError       = "This Field must be one of those: " + String.Join(" / ", Enum.GetNames(typeof(eColor)).ToList().Select(x => x.ToString()));
            string       carDoorError     = "This Field must be one of those: " + String.Join(" / ", Enum.GetNames(typeof(eCarDoors)).ToList().Select(x => x.ToString()));
            string       licenseTypeError = "This Field must be one of those: " + String.Join(" / ", Enum.GetNames(typeof(eLicenseType)).ToList().Select(x => x.ToString()));

            switch (i_Type)
            {
            case "int":
                intField = !int.TryParse(i_Value, out intField) ?
                           throw new FormatException("This Field must be a number") :
                                 intField < 0 ? throw new FormatException("Positive number only") : intField;
                break;

            case "eColor":
                colorField = !Enum.IsDefined(typeof(eColor), i_Value) ?
                             throw new FormatException(colorError) : colorField;
                break;

            case "eCarDoors":
                doorField = !Enum.IsDefined(typeof(eCarDoors), i_Value) ?
                            throw new FormatException(carDoorError) : doorField;
                break;

            case "eLicenseType":
                licenseTypeField = !Enum.IsDefined(typeof(eLicenseType), i_Value) ?
                                   throw new FormatException(licenseTypeError) : licenseTypeField;
                break;

            case "bool":
                boolField = !bool.TryParse(i_Value, out boolField) ? throw new FormatException("This Field must be a True of False") : boolField;
                break;

            case "float":
                floatField = !float.TryParse(i_Value, out floatField) ?
                             throw new FormatException("This Field must be a float") :
                                   floatField < 0 ? throw new FormatException("Positive number only") : floatField;
                break;

            case "string":
                if (i_Field == "License Plate")
                {
                    if (i_Garage.IsLicensePlateExists(i_Value))
                    {
                        throw new ArgumentException("There is already a Vehicle with the same license plate");
                    }
                }
                if (i_Value.Replace(" ", "").Length == 0)
                {
                    throw new FormatException("This Field can't be empty");
                }

                break;
            }

            return(true);
        }