Exemplo n.º 1
0
        /// <summary>
        /// Creates a new <see cref="ConversionValue"/> in the format of:  X <paramref name="remainingUnit"/> per 1 <paramref name="canceledUnit"/>.
        /// </summary>
        /// <param name="remainingUnit">The output unit, or the unit that remains after being multiplied with the input <see cref="ConversionValue"/>.</param>
        /// <param name="canceledUnit">The unit that will cancel when multiplied with the input <see cref="ConversionValue"/>.</param>
        /// <returns>A <see cref="ConversionValue"/> that converts <paramref name="canceledUnit"/> into <paramref name="remainingUnit"/>.</returns>
        public ConversionValue GetConversionValue(string canceledUnit, string remainingUnit)
        {
            var remainingUnitRowIndex = Table.GetColumnData(ConversionType).IndexOf(remainingUnit);
            var canceledUnitRowIndex  = Table.GetColumnData(ConversionType).IndexOf(canceledUnit);
            var conversionRatio       = new ConversionValue();

            if (remainingUnitRowIndex != -1 &&
                canceledUnitRowIndex != -1)
            {
                var cellData  = string.Empty;
                var cellValue = 0d;

                // Check if the conversionValue coordinates access the lower half of the conversionTable, if so, swap coordinates and invert the value.
                if (canceledUnitRowIndex <= remainingUnitRowIndex)
                {
                    cellData = Table.GetColumnData(canceledUnit).ElementAt(remainingUnitRowIndex).ToString();
                    double.TryParse(cellData, out cellValue);
                }
                else
                {
                    cellData = Table.GetColumnData(remainingUnit).ElementAt(canceledUnitRowIndex).ToString();
                    double.TryParse(cellData, out cellValue);
                    cellValue = 1 / cellValue;
                }

                conversionRatio = new ConversionValue(cellValue, new[] { remainingUnit }, new[] { canceledUnit }, ConversionType);
            }

            return(conversionRatio);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Multiplies two conversion values together.
        /// </summary>
        /// <param name="value1">The 'input' unit.</param>
        /// <param name="value2">The 'output' unit.</param>
        /// <returns>A condensed <see cref="ConversionValue"/> that takes on the simplest version of a combination of both units.</returns>
        public static ConversionValue operator *(ConversionValue value1, ConversionValue value2)
        {
            var conversionResult = new ConversionValue();

            conversionResult.Value = (value1?.Value ?? 0d) * (value2?.Value ?? 0d);
            conversionResult.UpperUnits.AddRange(value1?.UpperUnits);
            conversionResult.UpperUnits.AddRange(value2?.UpperUnits);
            conversionResult.LowerUnits.AddRange(value1?.LowerUnits);
            conversionResult.LowerUnits.AddRange(value2?.LowerUnits);
            conversionResult.Type = value1.Type;
            conversionResult.CancelUnits();

            return(conversionResult);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Compares equality between two conversion values.
        /// </summary>
        /// <param name="otherValue">Another conversion value to compare against.</param>
        /// <returns>The equality between two conversion values.</returns>
        public bool Equals(ConversionValue otherValue)
        {
            bool equalValues = false;

            if (otherValue != null)
            {
                if (Type.Equals(otherValue.Type) &&
                    Value == otherValue.Value &&
                    UpperUnits.All(unit => unit?.Equals(otherValue.UpperUnits.ElementAt(UpperUnits.IndexOf(unit))) ?? true) &&
                    LowerUnits.All(unit => unit?.Equals(otherValue.LowerUnits.ElementAt(LowerUnits.IndexOf(unit))) ?? true))
                {
                    equalValues = true;
                }
            }

            return(equalValues);
        }