Solved

negative values in case statements

Posted on 2010-11-09
6
681 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

697 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