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.
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.


