Thursday, October 2, 2014

C# Nested Switch Statement

Nested Switch C#Net code


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 nested switch logic can be reused in C++ and Java languages with a little language specific changes.

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 C# switch statement example, we have two text boxes and a button placed on a WinForm, 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# .Net 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 at the beginning of this article.


1 comment:

Feel free to talk back...