Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is it posible to make a switch statement look like this:

switch(i)
{
    case <= 50:
        {
            MessageBox.Show("i is less or = 50");
        }
    break;

}


instead of this..

switch(i)
{
    case 1:
    case 2: 
    case 3:
    case 4:
    ....:
    case 50:
        {
            MessageBox.Show("i is less or = 50");
        }
    break;
}
Posted
Updated 2-Dec-10 6:27am
v2
Comments
Toli Cuturicu 2-Dec-10 12:39pm    
No.

Well, if thats what you want then why not do this:
C#
if(i<=50)
   i = 50;
switch(i)
{    
  case 50:MessageBox.Show("i is less or = 50");        
          break;
  ....
  ....
}
 
Share this answer
 
Comments
jfriedman 2-Dec-10 13:36pm    
Manipulates i, thus losing the origional value. What is i's value is needed later?
Sandeep Mewara 2-Dec-10 13:39pm    
Common, it's all based on situation. If you really want to do something like this, and then you need real value... use temp variable. Assign value of i to some temporary variable and use switch on this temp variable!!!!
Quickest thing I can think of...

C#
int value = 9;

            bool circuit = value <= 50;

            switch (circuit)
            {
                case true:
                    {
                        Console.WriteLine("true");
                        break;
                    }
                case false:
                    {
                        Console.WriteLine("false");
                        break;
                    }
            }

            circuit = value <= 50 && value >= 40;

            switch (circuit)
            {
                case true:
                    {
                        Console.WriteLine("true");
                        break;
                    }
                case false:
                    {
                        Console.WriteLine("false");
                        break;
                    }
            }
 
Share this answer
 
Comments
Toli Cuturicu 2-Dec-10 12:38pm    
Absurdity or joke
jfriedman 2-Dec-10 13:35pm    
The only joke about this is that since there are only 2 viable states for a bool You can manipulate circuit based on conditionals and you only need the iif... Whats funny about that :p

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900