Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

C# Switch Statement [Intermediate Level Tutorial]

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;
            }
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.
Thanks for this, learned something new!
so this does the same thing as an if statment?
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.
(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.
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by dummycartSep 9, 2021 03:38 AM 1 146 Sep 9, 2021 03:38 AM
Last Post: Lindros
[Java] by OBAMASELFApr 23, 2019 12:42 AM 4 245 Apr 23, 2019 12:42 AM
Last Post: Joseph Stalin
[Python] by CiataLearnerDec 22, 2018 02:27 PM 1 212 Dec 22, 2018 02:27 PM
Last Post: notjapanese