244 lines
8.6 KiB
C#
244 lines
8.6 KiB
C#
/*
|
|
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
|
|
{
|
|
/// <summary>
|
|
/// A class to implement self-checking, on-error-auto-syntax-building
|
|
/// range-restricted int properties.
|
|
/// </summary>
|
|
public partial class RangeRestrictedIntProperty : Property<int>
|
|
{
|
|
/// <summary>
|
|
/// Determine whether or not the specified value is valid for this property.
|
|
/// </summary>
|
|
///
|
|
/// <param name="value">
|
|
/// The <see cref="int"/> value to be validity checked.
|
|
/// </param>
|
|
///
|
|
/// <returns>
|
|
/// <see cref="bool"/> true if the value is valid; false otherwise.
|
|
/// </returns>
|
|
///
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generate a user-readable explanation as to why the specified value is
|
|
/// not valid for this property.
|
|
/// </summary>
|
|
///
|
|
/// <param name="value">
|
|
/// The <see cref="int"/> value to be described.
|
|
/// </param>
|
|
///
|
|
/// <returns>
|
|
/// A <see cref="string"/> description explaining why the specified value
|
|
/// is not valid for this property.
|
|
/// </returns>
|
|
///
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The minimum <see cref="int"/> value permitted in this property.
|
|
/// </summary>
|
|
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<int> _MinimumValue = new Property<int>(0, false);
|
|
|
|
/// <summary>
|
|
/// The maximum <see cref="int"/> value permitted in this property.
|
|
/// </summary>
|
|
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<int> _MaximumValue = new Property<int>(0, false);
|
|
|
|
/// <summary>
|
|
/// Initialize an instance of a range-restricted integer property.
|
|
/// </summary>
|
|
///
|
|
/// <param name="minimumValue">
|
|
/// The minimum <see cref="int"/> value permitted in this property.
|
|
/// </param>
|
|
///
|
|
/// <param name="maximumValue">
|
|
/// The maximum <see cref="int"/> value permitted in this property.
|
|
/// </param>
|
|
///
|
|
/// <param name="initialValue">
|
|
/// The initial <see cref="int"/> value of this property.
|
|
/// </param>
|
|
///
|
|
/// <param name="isInitialized">
|
|
/// <see cref="bool"/> true if this property is to be considered
|
|
/// initialized after construction, false otherwise.
|
|
/// </param>
|
|
///
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initialize an instance of a range-restricted integer property.
|
|
/// </summary>
|
|
///
|
|
/// <param name="minimumValue">
|
|
/// The minimum <see cref="int"/> value permitted in this property.
|
|
/// </param>
|
|
///
|
|
/// <param name="maximumValue">
|
|
/// The maximum <see cref="int"/> value permitted in this property.
|
|
/// </param>
|
|
///
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initialize an instance of a range-restricted integer property.
|
|
/// </summary>
|
|
///
|
|
/// <param name="initialValue">
|
|
/// This initial <see cref="int"/> value of the property.
|
|
/// </param>
|
|
///
|
|
public RangeRestrictedIntProperty(int initialValue)
|
|
: this(int.MinValue, int.MaxValue, initialValue, true)
|
|
{
|
|
}
|
|
}
|
|
}
|