Tuesday, March 13, 2012

Switch case (C#)

How can a use a expression like (percent_ < 25) into the switch case?
It's returning me a error.

switch (percent_) {case(percent_ < 25): img.ImageUrl ="~/img/BarraHorizGreen.gif";break;case(percent_ > 40): img.ImageUrl ="~/img/BarraHorizRed.gif";break;default: img.ImageUrl ="~/img/BarraHorizYellow.gif"; }

Sorry, C# does not support fall-through cases, each case must be exact. ref:http://msdn2.microsoft.com/EN-US/library/aa664749(VS.71).aspx

Work around is easy, use if...else statement.


in VB it would be possible like..

Select Case XCase 126 to 500' any number between 126 and 500Case 5, 9, 125' 5 or 9 or 125Case is > 800' any number greater than 800Case Else'End Select
but i think it would nt be possible in C#.. you can use Else..IF  Ladder..
hope it helps./. 

0 comments:

Post a Comment