Glenn Patton

Glenn Patton

  • NA
  • 308
  • 82.3k

switch case range of values....

Jun 12 2012 7:29 AM
Hi All,

I'm trying to write a program that when it gets a piece of data back it will add a suitable padding to it to allow for processing.  I have a rough bit of code using if then, if() loops that works.  In the interest of this being maintainable later I am trying to use a switch() case the if() version is below:  I need it to test over a range of values as while each case falling to the next works for a few values when it gets to 100's it is not really possible. How do you do this properly I have done this in C I know I have!  
            //works!!!!
            if ((FileTotal < 9) || (FileTotal == 9))
                FileIDFiller = "000";
            if ((FileTotal < 99) && (FileTotal > 9))
                FileIDFiller = "00";
            if ((FileTotal < 999)&&(FileTotal >99))
                FileIDFiller = "0";
            if (FileTotal > 999)
                FileIDFiller = "";

the switch() version I have tried is below also:


            switch (Value)
            {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                    MessageBox.Show("000");
                    break;
                case 10:
                case 99:
                    MessageBox.Show("00");
                    break;
                case 100:
                case 999:
                    MessageBox.Show("0");
                    break;
                case 1000:
                    MessageBox.Show("");
                    break;
                default:
                    MessageBox.Show("Something broke!!");
                    break;


            } 

 There does not appear to be one!


Answers (12)