Example #1
0
        /// <summary>
        /// Calculate the incoming expression string with variables.
        /// </summary>
        /// <param name="values">The array of variables for an expression</param>
        /// <param name="targetType">The type of the binding target property. This is not implemented.</param>
        /// <param name="parameter">The expression to calculate.</param>
        /// <param name="culture">The culture to use in the converter. This is not implemented.</param>
        /// <returns>A <see cref="double"/> The result of calculating an expression.</returns>
        public object?Convert(object[]?values, Type?targetType, object?parameter, CultureInfo culture)
        {
            if (parameter is not string expression)
            {
                throw new ArgumentException("The parameter should be of type String.");
            }

            if (values == null)
            {
                return(null);
            }

            var args = new List <double>();

            foreach (var value in values)
            {
                if (value == null)
                {
                    return(null);
                }

                if (double.TryParse(value.ToString(), out var xValue))
                {
                    args.Add(xValue);
                }
            }

            var math = new MathExpression(expression, args);

            var result = math.Calculate();

            return(result);
        }
        /// <summary>
        /// Calculate the incoming expression string with one variable.
        /// </summary>
        /// <param name="value">The variable X for an expression</param>
        /// <param name="targetType">The type of the binding target property. This is not implemented.</param>
        /// <param name="parameter">The expression to calculate.</param>
        /// <param name="culture">The culture to use in the converter. This is not implemented.</param>
        /// <returns>A <see cref="double"/> The result of calculating an expression.</returns>
        public object?Convert(object?value, Type?targetType, object?parameter, CultureInfo culture)
        {
            if (parameter is not string expression)
            {
                throw new ArgumentException("The parameter should be of type String.");
            }

            if (value == null || !double.TryParse(value.ToString(), out var xValue))
            {
                return(null);
            }

            var math = new MathExpression(expression, new[] { xValue });

            var result = math.Calculate();

            return(result);
        }