Thursday, October 2, 2014

C# Nested Switch Statement

Switch statement is a very important programming language construct, appropriate usage can significantly speed up your code.
Keeping the importance of nested switch statement I've decided to put together a quick code example and make it available for the welfare of programmer community.

Example Background:
In case of this example, we have two text boxes and a button, when the user puts some text inside the first text box some text in second text box and then clicks the button our switch statement is fired in a button click handler.
The results are being printed using System.Diagnostics.Debug.Print function which cause them to show up in output console.
The user can enter 1, 2, or 3 in first text box and values a, b, or c in second text box. Anything else is handled in nested default sections.




C# nested switch might not be used very commonly, but you never know when you'll run into a problem that's better handled by the gimmick like consumption of the construct. The exact code of a nested switch statement in C# .Net programming language is given below.

Nested Switch C# code:


There you go!!!


private void button1_Click(object sender, EventArgs e)
        {
            switch(textBox1.Text){// outer switch statement c#
                case "1":
                    System.Diagnostics.Debug.Print("1");
                    switch (textBox2.Text)// inner switch statement c#
                    {
                        case "a":
                            System.Diagnostics.Debug.Print("1a");
                            break;
                        case "b":
                            System.Diagnostics.Debug.Print("1b");
                            break;
                        default:
                            System.Diagnostics.Debug.Print("1NA");
                            break;
                    }
                    break;
                case "2":
                    switch (textBox2.Text)// second nested switch statement c#.net
                    {
                        case "a":
                            System.Diagnostics.Debug.Print("2a");
                            break;
                        case "b":
                            System.Diagnostics.Debug.Print("2b");
                            break;
                        default:
                            System.Diagnostics.Debug.Print("2NA");
                            break;
                    }
                    break;
                case "3":
                    switch (textBox2.Text)// third nested switch statement in c#
                    {
                        case "a":
                            System.Diagnostics.Debug.Print("3a");
                            break;
                        case "b":
                            System.Diagnostics.Debug.Print("3b");
                            break;
                        default:
                            System.Diagnostics.Debug.Print("3NA");
                            break;
                    }
                    break;
                default:
                    switch (textBox2.Text)// default section of outer switch statement
                    {
                        case "a":
                            System.Diagnostics.Debug.Print("DEFAULTa");
                            break;
                        case "3b":
                            System.Diagnostics.Debug.Print("DEFAULTb");
                            break;
                        default:
                            System.Diagnostics.Debug.Print("DEFAULTNA");
                            break;
                    }
                    break;
            }
        }

Please note that the same logic can be reused in C++ and Java languages with a little language specific changes.


I'm working as a full time freelance programmer through ODesk.com, I have a strong profile over there. Feel free to ping me if you need an Android app. Elance widget is given on top right of the page, for ODesk follow the link given below:


https://www.odesk.com/users/~012d73aa92fad47188

No comments:

Post a Comment

Feel free to talk back...