Click here to Skip to main content
12,626,813 members (36,793 online)
Click here to Skip to main content
Add your own
alternative version

Stats

19.4K views
84 downloads
6 bookmarked
Posted

Creating a Switch function that works for non-integral types

, 4 Jun 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
A Switch function that works for non-integral types.

Introduction

Often when machines communicate with humans there is the need of switch statements. "The switch statement is a control statement that handles multiple selections and enumerations by passing control to one of the case statements within its body" (MSDN). These cases must always be constants and of an integral type. That means that doubles and floats can't be used and all other values must be constants.

This code uses templates and delegates to create a simple switch like structure that can be used for any type of case. The cases are sorted in a SortedList<TKey, TValue>.

The code uses anonymous methods to do the work in the case statements.

Using the code

In the sample code below, a message box is shown for each case.

Switch<string> switcher = new Switch<string>(); //Create Object
switcher.AddCase("Hello", delegate(object[] args)//Add cases
    { MessageBox.Show("Hello"); });
switcher.AddCase("Goodbye", delegate(object[] args)
    { MessageBox.Show("Goodbye"); });
switcher.AddCase("Morning", delegate(object[] args)
    { MessageBox.Show("Morning"); });
switcher.AddCase("Evening", delegate(object[] args)
    { MessageBox.Show("Evening"); });
switcher.AddCase("Afternoon", delegate(object[] args)
    { MessageBox.Show("Afternoon"); });
switcher.AddDefault(delegate (object [] args)
    { MessageBox.Show("Default"); });//Add a default case.
switcher.DoSwitch(StringTextBox.Text); //Do Switch

I made a class for doubles also. This version allows you to give a tolerance of how much the value can vary.

DoubleSwitch switcher = new DoubleSwitch();//Create Object
switcher.AddCase(1, delegate(object[] args)//Add cases
    { MessageBox.Show("1"); });
switcher.AddCase(2, delegate(object[] args)
    { MessageBox.Show("2"); });
switcher.AddCase(2.5, delegate(object[] args)
    { MessageBox.Show("2.5"); });
switcher.AddCase(3, delegate(object[] args)
    { MessageBox.Show("3"); });
switcher.AddCase(1000, delegate(object[] args)
    { MessageBox.Show("1000"); });
switcher.AddDefault(delegate (object [] args)
    { MessageBox.Show("Default"); });//Add a default case
switcher.DoSwitch((double) DoubleTextBox.Value, 0.1d); //Do switch.

The above code allows a tolerance of 0.1. That means that 2.59 will invoke the 2.5 case...

Further, specific classes are possible, they simply need to be derived from the base class and the function "DoSwitch" needs to be implemented.

Try the demo program to get a bit more of an idea...

History

This is the first version... Enjoy!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Joe Sonderegger
Software Developer RUAG Aerospace
Switzerland Switzerland
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 2 Pin
jkersch5-Jan-10 12:47
memberjkersch5-Jan-10 12:47 
GeneralInteresting Pin
Moim Hossain4-Jun-07 9:10
memberMoim Hossain4-Jun-07 9:10 
GeneralRe: Interesting Pin
Nice Life4-Jun-07 20:05
memberNice Life4-Jun-07 20:05 
Generalbad example Pin
dimzon29-May-07 1:38
memberdimzon29-May-07 1:38 
GeneralRe: bad example Pin
Nice Life29-May-07 1:50
memberNice Life29-May-07 1:50 
GeneralString switch statements legal in C# Pin
Rudolf Jan Heijink29-May-07 1:08
memberRudolf Jan Heijink29-May-07 1:08 
GeneralRe: String switch statements legal in C# Pin
Nice Life29-May-07 1:46
memberNice Life29-May-07 1:46 
GeneralRe: String switch statements legal in C# Pin
tanwinking31-Jul-07 23:33
membertanwinking31-Jul-07 23:33 
GeneralRe: String switch statements legal in C# Pin
Joe Sonderegger6-Aug-07 3:18
memberJoe Sonderegger6-Aug-07 3:18 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161205.3 | Last Updated 4 Jun 2007
Article Copyright 2007 by Joe Sonderegger
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid