using System; //AUS AvalonControlsLibrary namespace BeWo.Controls { /// /// Helper class that contains methods that execute mathematical operations /// public static class MathUtil { /// /// Validates the string passed by parsing it as int and checking if it is inside the bounds specified /// then the resulting int will be incremented/decremented /// /// The string to parse as int and increment/decrement /// The min value for the bound checking /// The max value for the bounds checking /// Pass true to increment and false to decrement /// Returns the new number incremented or decremeneted public static int IncrementDecrementNumber(string num, int minValue, int maxVal, bool increment) { int newNum = ValidateNumber(num, minValue, maxVal); if (increment) { newNum = Math.Min(newNum + 1, maxVal); } else { newNum = Math.Max(newNum - 1, 0); } return newNum; } /// /// Parses the number and makes sure that it is within the bounds specified /// /// The string to parse and validate /// The min value for the bound checking /// The max value for the bound checking /// Returns the int that was constructed from the string + bound checking public static int ValidateNumber(string newNum, int minValue, int maxValue) { int num; if (!int.TryParse(newNum, out num)) { return 0; } return ValidateNumber(num, minValue, maxValue); } /// /// makes sure that the number is within the bounds specified /// /// The number to validate /// The min value for the bound checking /// The max value for the bound checking /// Returns the int that was constructed from the string + bound checking public static int ValidateNumber(int newNum, int minValue, int maxValue) { newNum = Math.Max(newNum, minValue); newNum = Math.Min(newNum, maxValue); return newNum; } } }