So we have come to the part of the tutorial where we start thinking differently and more like a human being! As a matter of fact, I am gonna introduce a really interesting coding structure today which is going to be really useful in your coming developing days. Truth be told, it is like the if-else statement but it’s a combination of if-else statements, headed into one compact structure. This really is a very well-structured coding process. I fell in love with its uniqueness and step-by-step lucidity. I do hope you will feel the same once you see the code.
Suggested Reading :
- Beginner’s Guide to Windows Phone 7 App development
- Create Hello World Application in Windows Phone 7
- Different properties of Windows Phone 7 emulator
- Coding the Hello World Application in Windows Phone 7
- How files interact in Windows Phone 7 App development
- Declaring Variables in Windows Phone Application Development
- Accepting input & displaying it in Windows Phone Application Development
- Using If Else Statement in Windows Phone 7 App Development
- How to create a Calculator App for Windows Phone 7
Take a good look at the visuals.
You do this, I will do that!
1. If you will recall the if-else structure, one of the drawbacks which I think you will have found is that it can become pretty messy when you have lots of conditions to be taken into account. This might at some point confuse you and you might even end up messing up your well thought out code. We can deal with this by using a new structure. Let me give you a brief intro to the switch-case structure or simple the switch structure.
2. Yes, you guessed it right-it will contain cases, which will do what is assigned to each of them. Let me give you a simple real time example to show you how it works-
“Take a minute to turn around and see what people are doing. They are all busy at their respective jobs right? The National Security guards are busy protecting the country. The politicians are busy looting the country. Well, let’s not step into politics now. The point I want to state is that each person has a qualification to do a job. Like the guards are given training for years. The politicians are thieves from birth. (Damn, I just can’t stop blabbering about them!) So that means they have each satisfied condition/s each to get into that post. You can’t just make a politician go to the border and make him guard our country. That would be a disaster! They would loot our borders too!
Similarly, the switch-case has a similar scenario. Each case has a definite condition, which is compared with the input the user gives. This condition if satisfied by a particular case, that case is executed. Else it goes on with the next case and checks there to see if it satisfies the condition there. If nothing is satisfied, well then we have a default which works like the ‘else’ part and accepts every other ‘kind’ of input from the user.”
3. Let’s try to deduce the syntax of the switch-case structure-
switch (<expression>)
{
case 1:
{
job 1;
job 2;
……job n;
break;
}
case 2:
{
job 1;
job 2;
……job n;
break;
}
…..case m:
{
job 1;
job 2;
……job n;
break;
}
default:
{
job 1;
job 2;
……job n;
break;
}
}
You would have realized how good-looking the code is. Everything is placed just at its right place and you will feel like you are in control when you write a code like this. Now let us try to understand the coding structure by writing a code. This code will do the exact same thing that we performed with the if-else statement. I assume that you know how to do the design part and it’s the same as the app we constructed last time we dealt with the ‘if-else’ statement. For those who forgot, I will rewind it with a visual for you-

4. Let’s take a look at the code now-

You will realize that the user input is stored in the textbox1 that we created in the designing part. This will determine which case is executed. Now each case has its definite condition associated with it, in this case, I have given each case a numeric condition. Whenever the user enters a value to the textBox1, it will be evaluated to see if it matches any of the cases. If for example, the user enters a value 1, then the case “1” will be executed. Similarly, if the user enters a value 2, then the case “2” will be executed and so on. Now what if the user enters an invalid input, which satisfies none of the cases? Well, that is going to be dealt with by the default case.
There is also a presence of the ‘break’ keyword which is associated at each of the cases. Now this is used to transfer the control of the program out of the structure once the particular case has been executed. What happens if we remove this ‘break’ keyword? It will probably yield an error as the emulator cannot execute two cases at a time. So it is pretty necessary in the Windows Phone C# coding.
5. We will now check the app in our emulator-

As you can see, when I give the input as 1, the statement inside the case “1” is executed and displayed as output to the textblock2. Now the break keyword will transfer the control of the program outside the structure and hence the code stops executing. This will be true for all subsequent inputs.
Let’s see what happens if we give an invalid input.

As you can see, any other input which is greater than 3 will invoke the default case and this will execute the statement inside the default case. See if it’s true for any other inputs.
So that’s pretty much it for the switch structure. Hope you did get a good grasp about this really useful structure. You can use this structure when the coding becomes pretty confusing to track and you have a pretty good choice between the if-else and switch.
Thanks for reading again and happy creating! I will be back with more interesting stuff next time!




Thanks for sharing this info which will help for many and not doubt in that you will introduce with some new and attractive things when you will post another post on that topic.
Hi,
Thanks for your valuable comments. Yeah you can check out other post regarding this Windows Phone 7 App development.
wow nice post dude keep up the good work
Hi Aishwarya,
Thankz for the comment.s
Wow..!! Nice article dude. Congratzz…Keep it up. And I hope you are going to be the sole owner of Samsung Galaxy S2. :P
Hi Gautam Yerra,
Ha Ha Ha….!!!! Thankz bro.. Hope I would win.