0 Replies - 371 Views - Last Post: 14 July 2014 - 08:10 PM

#1 Rhino1111  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 99
  • View blog
  • Posts: 228
  • Joined: 28-August 13

Type / Int Switcher.

Posted 14 July 2014 - 08:10 PM

The Type Switcher is from this SO topic: http://stackoverflow...-switch-on-type

The Int Switcher is inspired by the Type Switcher, and follows the same pattern.

The point of these classes is to make switching on a Type or Integer easier than it currently is(and to make it more readable/easier to write).



Int Switcher:
public struct Range
    {
        public int Start;
        public int End;
        public Range(int start, int end)
        {
            Start = start;
            End = end;
        }
        internal bool IsWithin(int val)
        {
            if (val >= Start && val <= End)
                return true;
            else return false;
        }
    }
    public static class IntSwitcher
    {
        public class CaseInfo
        {
            public Range CaseCondition { get; set; }
            public Action Target { get; set; }
            public bool IsDefaultCase {get; set;}
        }
        public static void Do(int val, params CaseInfo[] cases) 
        {
            IntSwitcher.Do(val, false, cases);
        }
        public static void Do(int val, bool executeAllMetCases, params CaseInfo[] cases)
        {
            bool caseHit = false;
            foreach (CaseInfo c in cases)
            {
                if (c.IsDefaultCase || c.CaseCondition.IsWithin(val))
                {
                    if (caseHit && c.IsDefaultCase) continue;
                    c.Target();
                    caseHit = true;
                    if(!executeAllMetCases) break;
                }
            }
        }
        public static CaseInfo Case(int val, Action action)
        {
            return new CaseInfo()
            {
                CaseCondition = new Range(val, val),
                Target = action,
                IsDefaultCase = false
            };
        }
        public static CaseInfo Case(Range range, Action action)
        {
            return new CaseInfo()
            {
                CaseCondition = range,
                Target = action,
                IsDefaultCase = false
            };
        }
        public static CaseInfo Default(Action action)
        {
            return new CaseInfo()
            {
                Target = action,
                IsDefaultCase = true
            };
        }
    }




Example Usage:

Example 1:
            int val = 15;
            IntSwitcher.Do(val,
            IntSwitcher.Case(1, () => { MessageBox.Show("Case 1 Hit!"); }),
            IntSwitcher.Case(new Range(1, 20), () => { MessageBox.Show("Case 1-20 Hit!");}),
            IntSwitcher.Case(15, () => { MessageBox.Show("Case 15 Hit!"); }));



Example 2:
            int val = 15;
            IntSwitcher.Do(val, true,
            IntSwitcher.Case(1, () => { MessageBox.Show("Case 1 Hit!"); }),
            IntSwitcher.Case(new Range(1, 20), () => { MessageBox.Show("Case 1-20 Hit!");}),
            IntSwitcher.Case(15, () => { MessageBox.Show("Case 15 Hit!"); }));



The difference between the 2 Int Switcher examples, is if you insert the "true" flag(2nd example), it will execute all cases which meet the condition (except the default case if another case was hit, if no other case was hit then the default case will still run.) Without the true flag, it will only execute the first case which meets the condition, or the default case.




Type Switcher from the SO page:

Spoiler


Is This A Good Question/Topic? 0
  • +

Page 1 of 1