Go To with Switch Case Statement in .NET

No.of Views567
Bookmarked0 times
Downloads 
Votes0
By  pranay rana   On  23 Feb 2011 01:02:49
Tag : .NET Frameworks , Miscellaneous
In my application, I reach stage where I have to execute code of the Case 1 of the Switch..Case and if some condition is satisfied, I have to execute the code of Case 3 of Switch..Case.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Go To

Allows us to jump unconditionally when required and it's not recommended to use Go To heavily.

Switch..Case

Statement allows to execute the case block based on the value of the variable or passed in switch, i.e., allow to do the condition base programming.

Problem

In my application, I reach stage where I have to execute code of the Case 1 of the Switch..Case and if some condition is satisfied, I have to execute the code of Case 3 of Switch..Case.

Example

Switch(myvariable)
{case 1://statements
…........if ( expression ) execute case 3:break;case 2: ….. break; case 3: …...... break; }

Solution

First solution to this problem is copy the code of case 3 and put it in the if block of case 1.

Example

case 1://statements
…........if ( expression )//code of case 3
break;

But the problem with the above solution is that it makes code redundant.

Second solution is to create a function and put the code in that and then execute the code.

case 1://statements
…........if ( expression ) Case3Code();break; function Case3Code() { …. }

The problem with this solution is that I have to create one Extra function which is not needed.

Third solution is to make use of Go To in switch..caseblock.


switch(MyVariable)
{
 case 1:
 //statements
…........ if ( expression )goto case 3: break; case 2: ….. break; case 3: …...... break; }

So Go to in Switch..Case allows me to do the thing easily and in a maintainable way.

 
Sign Up to vote for this article
 
About Author
 
pranay rana
Occupation-CEO
Company-GMind Solusion
Member Type-Senior
Location-India
Joined date-08 Jan 2011
Home Page-http://pranayamr.blogspot.com
Blog Page-http://pranayamr.blogspot.com
Hey, I am Pranay Rana, working as a Senior Software engineer in mid-size company located in ahmedabad. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 4.3 years now. For me def. of programming is : Programming is something that you do once and that get used by multiple for many years You can visit me on my blog - http://pranayamr.blogspot.com/ StackOverFlow - http://stackoverflow.com/users/314488/pranay My CV :- http://careers.stackoverflow.com/pranayamr
 
 
Other popularSectionarticles
    Let us start this article by a small chat between customer and developer. Scenario 1 Customer: - How’s your application performance? Subjective developer: - Well it’s speedy, it’s the best …huuh aaa ooh it’s a like rocket. Scenario 2 Customer: - How’s your application performance? Quantitative developer: - With 2 GB RAM , xyz processor and 20000 customer records the customer screen load in 20 secs. I am sure the second developer looks more promising than the first developer. In this
    Published Date : 10/May/2010
    Ask any developer which is the best place in a .NET class to clean unmanaged resources?, 70% of them will say the destructor. Although it looks the most promising place for cleanup it has a huge impact on performance and memory consumption. Writing clean up code in the destructor leads to double GC visits and thus affecting the performance multifold times. In order to validate the same we will first start with bit of theory and then we will actually see how GC algorithm performance is impacte
    Published Date : 06/May/2010
    One of the important factors for performance degradation in .NET code is memory consumption. Many developers just concentrate on execution time to determine performance bottle necks in a .NET.
    Published Date : 05/May/2010
    This article will explain 6 important concepts Stack , heap , by val , by ref , boxing and unboxing. This article starts first explaining what happens internally when you declare a variable and then i
    Published Date : 02/May/2010
    Introduction Value Types in .Net Framework
    Published Date : 09/Apr/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top