Click here to Skip to main content
15,910,277 members
Home / Discussions / C#
   

C#

 
AnswerRe: Set license key / product in c# project - installation package. Pin
DJ-Boris12-Feb-09 0:31
DJ-Boris12-Feb-09 0:31 
GeneralRe: Set license key / product in c# project - installation package. Pin
thecodesource7912-Feb-09 13:52
thecodesource7912-Feb-09 13:52 
AnswerRe: Set license key / product in c# project - installation package. Pin
Member 114976512-Dec-09 0:53
Member 114976512-Dec-09 0:53 
QuestionStartup Performance Pin
Wizard_0111-Feb-09 11:00
Wizard_0111-Feb-09 11:00 
QuestionSomething better than Switch Case Pin
Muammar©11-Feb-09 10:12
Muammar©11-Feb-09 10:12 
AnswerRe: Something better than Switch Case Pin
akidan11-Feb-09 10:20
akidan11-Feb-09 10:20 
GeneralRe: Something better than Switch Case Pin
Muammar©11-Feb-09 10:28
Muammar©11-Feb-09 10:28 
AnswerRe: Something better than Switch Case Pin
akidan11-Feb-09 11:04
akidan11-Feb-09 11:04 
Yes, it should work with enums.

Again, though, it really depends a lot on what you're trying to do, so it's hard to recommend one approach over another... but here's a sample of some different techniques. Each have their strengths and weaknesses, depending on your situation.

Assuming an enum like:
enum MyEnum { Foo, Bar, Baz }

switch way:
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");
	}
}


Refactor to methods way:
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;
}


Dictionary way (.Invoke() is just for clarity):
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;
}


Subclass/override way (the enum actually goes away here):
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;
	}
}


Hope this helps ease your switch/case pain! Smile | :)

modified on Wednesday, February 11, 2009 5:21 PM

GeneralRe: Something better than Switch Case Pin
Pete O'Hanlon11-Feb-09 11:07
mvePete O'Hanlon11-Feb-09 11:07 
GeneralRe: Something better than Switch Case Pin
akidan11-Feb-09 11:11
akidan11-Feb-09 11:11 
GeneralRe: Something better than Switch Case Pin
Muammar©11-Feb-09 22:16
Muammar©11-Feb-09 22:16 
GeneralRe: Something better than Switch Case Pin
akidan12-Feb-09 3:10
akidan12-Feb-09 3:10 
AnswerRe: Something better than Switch Case Pin
Deresen11-Feb-09 10:45
Deresen11-Feb-09 10:45 
AnswerRe: Something better than Switch Case Pin
Pete O'Hanlon11-Feb-09 11:06
mvePete O'Hanlon11-Feb-09 11:06 
GeneralRe: Something better than Switch Case Pin
led mike11-Feb-09 11:29
led mike11-Feb-09 11:29 
GeneralRe: Something better than Switch Case Pin
akidan11-Feb-09 11:31
akidan11-Feb-09 11:31 
GeneralRe: Something better than Switch Case Pin
Muammar©11-Feb-09 22:27
Muammar©11-Feb-09 22:27 
GeneralRe: Something better than Switch Case Pin
J4amieC11-Feb-09 23:22
J4amieC11-Feb-09 23:22 
GeneralRe: Something better than Switch Case Pin
Muammar©12-Feb-09 0:44
Muammar©12-Feb-09 0:44 
AnswerRe: Something better than Switch Case Pin
Christian Graus11-Feb-09 11:37
protectorChristian Graus11-Feb-09 11:37 
QuestionRemove empty textbox spaces Pin
ferronrsmith11-Feb-09 9:35
ferronrsmith11-Feb-09 9:35 
GeneralRe: Remove empty textbox spaces Pin
Lodeclaw11-Feb-09 9:51
Lodeclaw11-Feb-09 9:51 
GeneralRe: Remove empty textbox spaces Pin
ferronrsmith11-Feb-09 10:19
ferronrsmith11-Feb-09 10:19 
AnswerRe: Remove empty textbox spaces [modified] Pin
Lodeclaw11-Feb-09 10:27
Lodeclaw11-Feb-09 10:27 
GeneralRe: Remove empty textbox spaces Pin
Deresen11-Feb-09 10:41
Deresen11-Feb-09 10:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.