Solved

negative values in case statements

Posted on 2010-11-09
6
623 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
Comment Utility
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
Comment Utility
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
Comment Utility

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:4Learning
Comment Utility

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
Comment Utility
Here is the actual error:

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

Author Comment

by:4Learning
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now