/* RangeRestrictedIntProperty.cs Copyright © 2008 Diversified Technical Systems, Inc. All Rights Reserved */ using System; using System.Collections.Generic; using System.Diagnostics; using System.Text; using DTS.Common.Utilities; namespace DTS.Common.Utilities.DotNetProgrammingConstructs { /// /// A class to implement self-checking, on-error-auto-syntax-building /// range-restricted int properties. /// public partial class RangeRestrictedIntProperty : Property { /// /// Determine whether or not the specified value is valid for this property. /// /// /// /// The value to be validity checked. /// /// /// /// true if the value is valid; false otherwise. /// /// public override bool IsValidValue(int value) { try { return value >= MinimumValue && value <= MaximumValue; } catch (System.Exception ex) { throw new Exception( string.Format(Properties.Resources.RangeRestrictedIntProperty_IsValidValue_UnableToDetermineValidityString, value.ToString()), ex); } } /// /// Generate a user-readable explanation as to why the specified value is /// not valid for this property. /// /// /// /// The value to be described. /// /// /// /// A description explaining why the specified value /// is not valid for this property. /// /// public override string GetInvalidValueDescription(int value) { try { if (value < MinimumValue) return string.Format(Properties.Resources.RangeRestrictedIntProperty_GetInvalidValueDescription_MinimumDescriptionString, value.ToString(), MinimumValue.ToString()); else if (value > MaximumValue) return string.Format(Properties.Resources.RangeRestrictedIntProperty_GetInvalidValueDescription_MaximumDescriptionString, value.ToString(), MaximumValue.ToString()); else return string.Format(Properties.Resources.RangeRestrictedIntProperty_GetInvalidValueDescription_ValidValueDescriptionString, value.ToString()); } catch (System.Exception ex) { throw new Exception( string.Format(Properties.Resources.RangeRestrictedIntProperty_GetInvalidValueDescription_GetDescriptionFailedString, value.ToString()), ex); } } /// /// The minimum value permitted in this property. /// public int MinimumValue { get { try { return _MinimumValue.Value; } catch (System.Exception ex) { throw new Exception(Properties.Resources.RangeRestrictedIntProperty_MinimumValue_GetValueFailedString, ex); } } set { try { _MinimumValue.Value = value; } catch (System.Exception ex) { throw new Exception(Properties.Resources.RangeRestrictedIntProperty_MinimumValue_SetValueFailedString, ex); } } } private Property _MinimumValue = new Property(0, false); /// /// The maximum value permitted in this property. /// public int MaximumValue { get { try { return _MaximumValue.Value; } catch (System.Exception ex) { throw new Exception(Properties.Resources.RangeRestrictedIntProperty_MaximumValue_GetValueFailedString, ex); } } set { try { _MaximumValue.Value = value; } catch (System.Exception ex) { throw new Exception(Properties.Resources.RangeRestrictedIntProperty_MaximumValue_SetValueFailedString, ex); } } } private Property _MaximumValue = new Property(0, false); /// /// Initialize an instance of a range-restricted integer property. /// /// /// /// The minimum value permitted in this property. /// /// /// /// The maximum value permitted in this property. /// /// /// /// The initial value of this property. /// /// /// /// true if this property is to be considered /// initialized after construction, false otherwise. /// /// public RangeRestrictedIntProperty(int minimumValue, int maximumValue, int initialValue, bool isInitialized) : base(initialValue, isInitialized) { try { if (minimumValue > maximumValue) throw new InvalidRangeException( string.Format( Properties.Resources.RangeRestrictedIntProperty_MinMustBeLessThanMaxString, minimumValue.ToString(), maximumValue.ToString())); else { MinimumValue = minimumValue; MaximumValue = maximumValue; if (!IsValidValue(initialValue)) throw new InvalidValueException(GetInvalidValueDescription(initialValue)); else return; } } catch (System.Exception ex) { throw new ConstructionException( string.Format(Properties.Resources.Generic_EncounteredProblemConstructingClassString, GetType().FullName), ex); } } /// /// Initialize an instance of a range-restricted integer property. /// /// /// /// The minimum value permitted in this property. /// /// /// /// The maximum value permitted in this property. /// /// public RangeRestrictedIntProperty(int minimumValue, int maximumValue) : base(0, false) { try { if (minimumValue > maximumValue) throw new InvalidRangeException( string.Format( Properties.Resources.RangeRestrictedIntProperty_MinMustBeLessThanMaxString, minimumValue.ToString(), maximumValue.ToString())); else { MinimumValue = minimumValue; MaximumValue = maximumValue; } } catch (System.Exception ex) { throw new ConstructionException( string.Format( Properties.Resources.Generic_EncounteredProblemConstructingClassString, GetType().FullName), ex); } } /// /// Initialize an instance of a range-restricted integer property. /// /// /// /// This initial value of the property. /// /// public RangeRestrictedIntProperty(int initialValue) : this(int.MinValue, int.MaxValue, initialValue, true) { } } }