Solved

negative values in case statements

Posted on 2010-11-09
6
663 Views
Last Modified: 2012-05-10
Hello experts

Can a negative value be passed to a switch/case statement in C#, eg:

int Val;
switch(Val)
case -1:
      //val=-1
      break;
case 1:
      //val=1
      break;
default:
      //something else
      break;

From what I have been trying, it looks like it cannot, then what is the alternative since I need to pass -1

Thanks,
0
Comment
Question by:4Learning
6 Comments
 
LVL 16

Assisted Solution

by:BurnieP
BurnieP earned 166 total points
ID: 34097872
You can convert it as string :
 

int Val;
switch(Val.ToString())
case "-1":
      //val=-1
      break;
case "1":
      //val=1
      break;
default:
      //something else
      break;

Open in new window

0
 
LVL 3

Assisted Solution

by:JamesAperta
JamesAperta earned 167 total points
ID: 34097964
yes a negative value can be passed in.  The following will print "minus 1" on my console window

int Val = -1;

switch (Val)
{
    case -1: Console.WriteLine("minus 1"); break;
    case 0: Console.WriteLine("zero"); break;
    default: Console.WriteLine("unhandled"); break;
}
0
 
LVL 16

Accepted Solution

by:
kris_per earned 167 total points
ID: 34101261

You need to correct two things in the code:

1. put the case statements inside { }

2. initialize Val to some value; otherwise you will get 'use of unassigned variable' error.

Your fixed code will look as following:
            int Val = -1;
            switch (Val)
            {
                case -1:
                    //val=-1
                    break;
                case 1:
                    //val=1
                    break;
                default:
                    //something else
                    break;
            }

Open in new window

0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:4Learning
ID: 34102533

for some reason, the compiler returns the error, cannot pass through case -1:

And whereever I put this piece of code, the error line number always point to the same.
0
 

Author Comment

by:4Learning
ID: 34102696
Here is the actual error:

Control cannot fall through from one case label ('case "-1":')  to another
0
 

Author Comment

by:4Learning
ID: 34102948
I found out the problem. That was the only case statement where I had missed putting a break statement.

Thanks to all the experts for your time!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question