Ant
no notes

Cyclical Ranged Number

This is a number which has constraints to make sure that its never greater than a given value or less that another given value.

It cycles around the minimum and maximum when incremented, decremented, added or subtracted, multiplied or what ever.

So it acts like Seconds or Minutes or Days of the Week.

Here is an example of a ZeroToSix, to make it your own change the MinValue and MaxValueto suit and call it something to match

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

/// <summary>
/// <para>ZeroToSix structure</para>
/// <para>Value can only be 0-6, increment/decrement is cyclical</para>
/// </summary>
public struct ZeroToSix
    : IComparable, IFormattable, IConvertible,
        IComparable<ZeroToSix>, IEquatable<ZeroToSix> {
    private byte _Value;

    /// <summary>
    /// <para>Minimum value</para>
    /// </summary>
    public const byte MinValue = 0;
    /// <summary>
    /// <para>Maximum value</para>
    /// </summary>
    public const byte MaxValue = 6;

    /// <summary>
    /// <para>Constructor</para>
    /// </summary>
    private ZeroToSix(int value) {
        // make sure value is in range
        Math.DivRem(value, MaxValue + 1 - MinValue, out value);
        if (value < MinValue) {
            value += (MaxValue + 1 - MinValue);
        }
        this._Value = (byte)value;
    }

    /// <summary>
    /// <para>Hash code</para>
    /// <para>Uses the one from the byte type</para>
    /// </summary>
    public override int GetHashCode() {
        return this._Value.GetHashCode();
    }

    /// <summary>
    /// <para>Check for equality</para>
    /// </summary>
    public override bool Equals(object obj) {
        return this._Value.Equals(obj);
    }

    /// <summary>
    /// <para>String representation</para>
    /// </summary>
    public override string ToString() {
        return this._Value.ToString();
    }

    /// <summary>
    /// <para>Parse a string to a ZeroToSix</para>
    /// </summary>
    public static ZeroToSix Parse(string s) {
        return new ZeroToSix(int.Parse(s));
    }

    /// <summary>
    /// <para>Parse a string to a ZeroToSix</para>
    /// </summary>
    public static ZeroToSix Parse(string s, IFormatProvider provider) {
        return new ZeroToSix(int.Parse(s, provider));
    }

    /// <summary>
    /// <para>Parse a string to a ZeroToSix</para>
    /// </summary>
    public static ZeroToSix Parse(string s, NumberStyles style) {
        return new ZeroToSix(int.Parse(s, style));
    }

    /// <summary>
    /// <para>Parse a string to a ZeroToSix</para>
    /// </summary>
    public static ZeroToSix Parse(string s, NumberStyles style, IFormatProvider provider) {
        return new ZeroToSix(int.Parse(s, style, provider));
    }

    /// <summary>
    /// <para>Attempt to parse a string to a ZeroToSix</para>
    /// </summary>
    public static bool TryParse(string s, out ZeroToSix result) {
        int parsedInt;

        if (int.TryParse(s, out parsedInt)) {
            try {
                result = new ZeroToSix(parsedInt);
                return true;
            } catch (ArgumentOutOfRangeException) { }
        }

        result = default(ZeroToSix);

        return false;
    }

    /// <summary>
    /// <para>Attempt to parse a string to a ZeroToSix</para>
    /// </summary>
    public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out ZeroToSix result) {
        int parsedInt;

        if (int.TryParse(s, style, provider, out parsedInt)) {
            try {
                result = new ZeroToSix(parsedInt);
                return true;
            } catch (ArgumentOutOfRangeException) { }
        }

        result = default(ZeroToSix);

        return false;
    }

    /// <summary>
    /// <para>Incremental</para>
    /// </summary>
    public static ZeroToSix operator +(ZeroToSix value, int addValue) {
        return new ZeroToSix(value + addValue);
    }

    /// <summary>
    /// <para>Decremental</para>
    /// </summary>
    public static ZeroToSix operator -(ZeroToSix value, int subValue) {
        return new ZeroToSix(value - subValue);
    }

    /// <summary>
    /// <para>Allow implicit convertion from an int</para>
    /// </summary>
    public static implicit operator ZeroToSix(int value) {
        return new ZeroToSix(value);
    }

    /// <summary>
    /// <para>Allow implicit convertion to a int</para>
    /// </summary>
    public static implicit operator int(ZeroToSix value) {
        return value._Value;
    }

    /// <summary>
    /// <para>Check equality</para>
    /// </summary>
    public bool Equals(ZeroToSix other) {
        return _Value.Equals(other);
    }

    /// <summary>
    /// <para>Compare ZeroToSixs</para>
    /// </summary>
    public int CompareTo(ZeroToSix other) {
        return _Value.CompareTo(other);
    }

    /// <summary>
    /// <para>Get the typecode, same as Byte object</para>
    /// </summary>
    public TypeCode GetTypeCode() {
        return _Value.GetTypeCode();
    }

    /// <summary>
    /// <para>Convert to a Boolean</para>
    /// </summary>
    public bool ToBoolean(IFormatProvider provider) {
        return Convert.ToBoolean(_Value, provider);
    }

    /// <summary>
    /// <para>Convert to a Byte</para>
    /// </summary>
    public byte ToByte(IFormatProvider provider) {
        return Convert.ToByte(_Value, provider);
    }

    /// <summary>
    /// <para>Convert to a Char</para>
    /// </summary>
    public char ToChar(IFormatProvider provider) {
        return Convert.ToChar(_Value, provider);
    }

    /// <summary>
    /// <para>Convert to a DateTime</para>
    /// </summary>
    public DateTime ToDateTime(IFormatProvider provider) {
        return Convert.ToDateTime(_Value, provider);
    }

    /// <summary>
    /// <para>Convert to a Decimal</para>
    /// </summary>
    public decimal ToDecimal(IFormatProvider provider) {
        return Convert.ToDecimal(_Value, provider);
    }

    /// <summary>
    /// <para>Convert to a Double</para>
    /// </summary>
    public double ToDouble(IFormatProvider provider) {
        return Convert.ToDouble(_Value, provider);
    }

    /// <summary>
    /// <para>Convert to an Int16</para>
    /// </summary>
    public short ToInt16(IFormatProvider provider) {
        return Convert.ToInt16(_Value, provider);
    }

    /// <summary>
    /// <para>Convert to an Int32</para>
    /// </summary>
    public int ToInt32(IFormatProvider provider) {
        return Convert.ToInt32(_Value, provider);
    }

    /// <summary>
    /// <para>Convert to an Int64</para>
    /// </summary>
    public long ToInt64(IFormatProvider provider) {
        return Convert.ToInt64(_Value, provider);
    }

    /// <summary>
    /// <para>Convert to an SByte</para>
    /// </summary>
    public sbyte ToSByte(IFormatProvider provider) {
        return Convert.ToSByte(_Value, provider);
    }

    /// <summary>
    /// <para>Convert to a Single</para>
    /// </summary>
    public float ToSingle(IFormatProvider provider) {
        return Convert.ToSingle(_Value, provider);
    }

    /// <summary>
    /// <para>Convert to a string</para>
    /// </summary>
    public string ToString(IFormatProvider provider) {
        return _Value.ToString(provider);
    }

    /// <summary>
    /// <para>Convert to an other type</para>
    /// </summary>
    public object ToType(Type conversionType, IFormatProvider provider) {
        return Convert.ChangeType(_Value, conversionType, provider);
    }

    /// <summary>
    /// <para>Convert to a UInt16</para>
    /// </summary>
    public ushort ToUInt16(IFormatProvider provider) {
        return Convert.ToUInt16(_Value, provider);
    }

    /// <summary>
    /// <para>Convert to a UInt32</para>
    /// </summary>
    public uint ToUInt32(IFormatProvider provider) {
        return Convert.ToUInt32(_Value, provider);
    }

    /// <summary>
    /// <para>Convert to a UInt64</para>
    /// </summary>
    public ulong ToUInt64(IFormatProvider provider) {
        return Convert.ToUInt64(provider);
    }

    /// <summary>
    /// <para>To string given a format provider</para>
    /// </summary>
    public string ToString(string format, IFormatProvider formatProvider) {
        return _Value.ToString(format, formatProvider);
    }

    /// <summary>
    /// <para>Compare to another object</para>
    /// </summary>
    public int CompareTo(object obj) {
        return _Value.CompareTo(obj);
    }
}

Post a Note

(required)

(required never shown)

On Twitter Follow MrAntix on Twitter

11 hours ago
verge
Microsoft teases Windows 8 'Consumer Preview' with Bing betta fish site http://t.co/lcJICazH