Hack Forums
[C#] C# Switch Statement [Intermediate Level Tutorial] - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] C# Switch Statement [Intermediate Level Tutorial] (/showthread.php?tid=756180)



C# Switch Statement [Intermediate Level Tutorial] - Pulser - Oct 21, 2010

C# Switch Case



What Is It:
A lot of the time someone will use a lot of "If Statements" or "Else If Statements". Personally, I think that doing that is a waste of space, and time. Instead, use a Switch Statement!

A switch statement is similar to an if statement in the way that it will check if an option is true. Basically, its like a bunch of if statements.

Getting Started:
To begin a switch statement, setup a string variable, create a button and create a textBox.

Set your string variable equal to your TextBox's text.

Code
string word = textBox1.Text;

Next, begin your switch statement.

Code
switch (word)
            {

Word is the name of our variable.
Switch says that it is a switch statement, and that we will be testing our variable!


We will have 1 possibility for each case. For each different case there will be a block of code that will be executed.

Code
case "Hack":
                    MessageBox.Show("Hack!");
                    break;
                case "forums":
                    MessageBox.Show("Forums!");
                    break;


case "Hack": is the case, "Hack" is the text inside of the variable that must be there for this case to "activate".

Underneath the case is the code we want to execute if the condition matches.

Break; simply allows C# to break from the switch.



Now, it is a good idea to use a "Default:". This is used in case none of your other cases match. To do this we simply type:

Code
default:
                    MessageBox.Show("Default!");
                    break;



Conclusion:
Switch statements are a great thing to use. If you want to account for multiple outcomes or values I highly suggest you try using them!


Code:
Code
string word = textBox1.Text;

            switch (word)
            {
                default:
                    MessageBox.Show("Default!");
                    break;
                case "Hack":
                    MessageBox.Show("Hack!");
                    break;
                case "forums":
                    MessageBox.Show("Forums!");
                    break;
                case "Predictable":
                    MessageBox.Show("Is cool!");
                    break;
            }



* - tflo - Oct 25, 2010

Thanks for this, learned something new!


* - Mr. Anderson - Oct 25, 2010

so this does the same thing as an if statment?


* - BaussHacker - Oct 25, 2010

switch is my love methode in c#.
Ive used it for sooo many things.
for games npcs, commands (player,controlpanel,cmd)

switch<3
cases<3

@anderson
using if and else, takes more memory use, then using cases.


* - Pulser - Oct 25, 2010

(Oct 25, 2010 02:56 PM)Mr. Anderson Wrote:  so this does the same thing as an if statment?

Possibly the same concept, it is closer related to Else If.