Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
4.60/5 (3 votes)
See more:
Hi,
I'm just sick of this lengthy switch case drama and I have a feeling there are already better solutions out there, if not, please help me with a creative way you may have used once to make it easier and shorter..

Many thanks for sharing guys!

Posted

You have a few options... it depends on the situation, really.

Assuming a case like:
C#
enum MyEnum { Foo, Bar, Baz }

bool method(MyEnum val)
{
    switch (val)
    {
        case MyEnum.Foo:
            Console.WriteLine("Some");
            Console.WriteLine("big");
            Console.WriteLine("case");
            Console.WriteLine("statement");
            Console.WriteLine("that");
            Console.WriteLine("Foo");
            Console.WriteLine("does");
            return true;
        case MyEnum.Bar:
            throw new Exception("Bar!");
        case MyEnum.Baz:
            Console.Beep();
            Console.WriteLine("Beep.");
            return false;
        default:
            throw new ArgumentOutOfRangeException("val");
    }
}


One possibility is to take the bodies of the case statements and make methods out of them.

C#
bool method(MyEnum val)
{
    switch (val)
    {
        case MyEnum.Foo:
            return FooMethod();
        case MyEnum.Bar:
            return BarMethod();
        case MyEnum.Baz:
            return BazMethod();
        default:
            throw new ArgumentOutOfRangeException("val");
    }
}

bool FooMethod()
{
    Console.WriteLine("Some");
    Console.WriteLine("big");
    Console.WriteLine("case");
    Console.WriteLine("statement");
    Console.WriteLine("that");
    Console.WriteLine("Foo");
    Console.WriteLine("does");
    return true;
}

bool BarMethod()
{
    throw new Exception("Bar!");
}

bool BazMethod()
{
    Console.Beep();
    Console.WriteLine("Beep.");
    return false;
}


Another is to create a dictionary of key to delegate, and do a dictionary lookup/invoke in place of a switch.

C#
delegate bool BoolFunc();
readonly Dictionary<MyEnum, BoolFunc> methods;

MyClassConstructor()
{
    methods = new Dictionary<MyEnum, BoolFunc>();
    methods.Add(MyEnum.Foo, FooMethod);
    methods.Add(MyEnum.Bar, BarMethod);
    methods.Add(MyEnum.Baz, BazMethod);
}

bool method(MyEnum val)
{
    BoolFunc funcToCall;

    if (!methods.TryGetValue(val, out funcToCall))
        throw new ArgumentOutOfRangeException("val");

    return funcToCall.Invoke();
}

bool FooMethod()
{
    Console.WriteLine("Some");
    Console.WriteLine("big");
    Console.WriteLine("case");
    Console.WriteLine("statement");
    Console.WriteLine("that");
    Console.WriteLine("Foo");
    Console.WriteLine("does");
    return true;
}

bool BarMethod()
{
    throw new Exception("Bar!");
}

bool BazMethod()
{
    Console.Beep();
    Console.WriteLine("Beep.");
    return false;
}


Another is to look for an object-oriented way where you differentiate behavior by overriding an abstract/virtual method, in place of a switch statement somewhere.

C#
bool method(MyNonEnumClass val)
{
    return val.DoYourThing();
}

abstract class MyNonEnumClass
{
    public abstract bool DoYourThing();
}

class FooClass : MyNonEnumClass
{
    public override bool DoYourThing()
    {
        Console.WriteLine("Some");
        Console.WriteLine("big");
        Console.WriteLine("case");
        Console.WriteLine("statement");
        Console.WriteLine("that");
        Console.WriteLine("Foo");
        Console.WriteLine("does");
        return true;
    }
}

class BarClass : MyNonEnumClass
{
    public override bool DoYourThing()
    {
        throw new Exception("Bar!");
    }
}

class BazClass : MyNonEnumClass
{
    public override bool DoYourThing()
    {
        Console.Beep();
        Console.WriteLine("Beep.");
        return false;
    }
}



All depends, though. :)
 
Share this answer
 
v2
You could always use a Dictionary with an enumeration and a Action to use. For instance, you could have an enumeration that closed a window like this:
public enum Operation <br />{<br />  Close,<br />  Save,<br />}<br />public void Action(Operation action)<br />{<br />  if (_dictionary.ContainsKey(action))<br />  {<br />    _dictionary[action]();<br />  }<br />}<br />public void Register(Operation operation, Action action)<br />{<br />  _dictionary.Add(operation, action);<br />}
Then, you can add your implementation like this:
Register(Operation.Close, delegate(){ this.Close(); });
As you can see, calling this method removes the need for a switch altogether.

 
Share this answer
 


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900