示例#1
0
 public bool Equals(RangedInt other)
 {
     return(Equals(OnBoundaryExceeded, other.OnBoundaryExceeded) &&
            Value == other.Value &&
            MinValue == other.MinValue &&
            MaxValue == other.MaxValue);
 }
示例#2
0
 // TODO: Review this experimental method. If keep it need to add overloads that support setting from other types and include in other Ranged Types too.
 public bool TrySetValue(int newValue, out RangedInt result)
 {
     if (newValue < MinValue || newValue > MaxValue)
     {
         result = null;
         return(false);
     }
     else
     {
         result = new RangedInt(newValue, MinValue, MaxValue, OnBoundaryExceeded);
         return(true);
     }
 }
示例#3
0
        public static RangedInt operator -(RangedInt original, RangedInt subtraction)
        {
            var result = new RangedInt(minValue: original.MinValue, maxValue: original.MaxValue, onBoundaryExceeded: original.OnBoundaryExceeded);

            if (int.MaxValue - original.Value < subtraction.Value)
            {
                result.SetValue(original.MinValue);
                result.OnBoundaryExceeded?.Invoke(new BoundaryExceededArgs($"{original.Value} - {subtraction.Value} < int.MinValue"));
            }
            else
            {
                result.SetValue(original.Value - subtraction.Value);
            }

            return(result);
        }
示例#4
0
        // TODO: test adding negative numbers
        public static RangedInt operator +(RangedInt original, int addition)
        {
            var result = new RangedInt(minValue: original.MinValue, maxValue: original.MaxValue, onBoundaryExceeded: original.OnBoundaryExceeded);

            if (int.MaxValue - original.Value < addition)
            {
                result.SetValue(original.MaxValue);
                result.OnBoundaryExceeded?.Invoke(new BoundaryExceededArgs($"{original.Value} + {addition} > int.MaxValue"));
            }
            else
            {
                result.SetValue(original.Value + addition);
            }

            return(result);
        }