Exemple #1
0
        static void Main(string[] args)
        {
            var measurementDictionary = new Dictionary <string, LinearQuantity>
            {
                { "Inch", new LinearQuantity(1.0, "Inch", Type.Length) },
                { "Foot", new LinearQuantity(12.0, "Inch", Type.Length) },
                { "Meter", new LinearQuantity(40.0, "Inch", Type.Length) },
                { "Pound", new LinearQuantity(453.592, "Gram", Type.Weight) },
                { "Gram", new LinearQuantity(1.0, "Gram", Type.Weight) }
            };

            var q = new LinearQuantity(24.0, "Inch", Type.Length);

            try
            {
                Console.Write(q.Value + " " + q.Unit + " = ");
                q = q.Convert("Foot", measurementDictionary);
                Console.WriteLine(q.Value + " " + q.Unit);

                Console.Write(q.Value + " " + q.Unit + " = ");
                q = q.Convert("Meter", measurementDictionary);
                Console.WriteLine(q.Value + " " + q.Unit);

                Console.Write(q.Value + " " + q.Unit + " = ");
                q = q.Convert("Pound", measurementDictionary);
                Console.WriteLine(q.Value + " " + q.Unit);
            }
            catch (InvalidDataException ide)
            {
                Console.WriteLine(ide.Message);
            }
        }
Exemple #2
0
        public LinearQuantity Convert(string toUnit, Dictionary <string, LinearQuantity> conversionMap)
        {
            if (!conversionMap.ContainsKey(Unit) || !conversionMap.ContainsKey(toUnit) || (Type != conversionMap[toUnit].Type))
            {
                throw  new InvalidDataException("Data not provided! Cannot convert.");
            }

            var currentToCommonValue  = conversionMap[Unit].Value;
            var requiredToCommonValue = conversionMap[toUnit].Value;
            var quantity = new LinearQuantity(Value * currentToCommonValue / requiredToCommonValue, toUnit, Type);

            return(quantity);
        }