using System; namespace Wayne.ForecourtControl { /// /// Price group stucture. Contains an underlying int between 0 and 11. /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes")] public struct PriceGroup { #region Fields int value; #endregion #region Construction [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId = "System.ArgumentException.#ctor(System.String)")] private PriceGroup(int newValue, bool forceValue) { if ((newValue >= MinValue) && (newValue <= MaxValue) || forceValue) { value = newValue; } else throw new ArgumentException( "Price group must be within the bounds " + MinValue.ToString(System.Globalization.CultureInfo.InvariantCulture) + "-" + MaxValue.ToString(System.Globalization.CultureInfo.InvariantCulture )); } /// /// /// /// public PriceGroup(int newValue) : this(newValue, false) { } #endregion #region Methods /// /// Equality comparer. /// /// /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2231:OverloadOperatorEqualsOnOverridingValueTypeEquals")] public override bool Equals(object obj) { if (obj is PriceGroup) return (int)((PriceGroup)obj) == this.value; else return false; } /// /// Hash Code generator. /// /// public override int GetHashCode() { return value.GetHashCode(); } #endregion #region Debug methods /// /// Presents the class as a string. /// /// public string ToString(string format, IFormatProvider provider) { string extraText = " "; switch (value) { case -1: extraText = " Unknown"; break; case 0: extraText = " FullService"; break; case 1: extraText = " PayInKiosk"; break; case 2: extraText = " OptCard"; break; case 3: extraText = " OptCash"; break; } return value.ToString(format, provider) + extraText; } /// /// Presents the class as a string using the specified culture-specific format information. /// /// public string ToString(IFormatProvider provider) { return ToString("", provider); } /// /// Presents the class as a string using a format string. /// /// public string ToString(string format) { return ToString(format, System.Globalization.CultureInfo.InvariantCulture); } /// /// Presents the class as a string using a format string and the specified culture-specific format information. /// /// public override string ToString() { return ToString("", System.Globalization.CultureInfo.InvariantCulture); } #endregion #region Conversions /// /// /// /// /// public static implicit operator int(PriceGroup thePriceGroup) { return thePriceGroup.value; } /// /// /// /// /// public static implicit operator PriceGroup(int priceGroupId) { return new PriceGroup(priceGroupId); } #endregion #region Public constants /// /// /// public const int MinValue = 0; /// /// /// public const int MaxValue = 11; /// /// /// public static readonly PriceGroup FullService = 0; /// /// /// public static readonly PriceGroup PayInKiosk = 1; /// /// /// public static readonly PriceGroup OptCash = 2; /// /// /// public static readonly PriceGroup OptCard = 3; /// /// /// public static readonly PriceGroup Unknown = new PriceGroup(-1, true); #endregion } }