Glenn Patton

Glenn Patton

  • NA
  • 308
  • 82.6k

Switch statement not working....

Oct 9 2012 3:54 AM
Hi All,

In writing an application I have come across a bug in a switch case statement, if I use an if else if it works translate that to a switch it does not, see below example.....

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//why does the switch not work?


namespace SwitchTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click( object sender, EventArgs e)
{
int NtoDown = 0;
string strNtoDown = null ;
string NumRecordFiller = "" ;

strNtoDown = textBox1.Text;
NtoDown = Convert .ToInt16(strNtoDown);
// MessageBox.Show(NtoDown.ToString());
//NtoDown = 5;
MessageBox .Show(NtoDown.ToString());
/* switch(NtoDown)
{
case 0-9:
MessageBox.Show("Hi");
NumRecordFiller = "000";
break;

case 10-99:
NumRecordFiller = "00";
break;

case 100-999:
NumRecordFiller = "0";
break;

}*/
//MessageBox.Show("NumRecordFiller = " + NumRecordFiller.ToString() + "HELLO");
if (NtoDown <= 9)
NumRecordFiller = "000";
else if (NtoDown <= 99)
NumRecordFiller = "00" ;

else if (NtoDown <= 999)
NumRecordFiller = "0" ;

else if (NtoDown >= 999)
NumRecordFiller = "" ;

MessageBox .Show( "NumRecordFiller = " + NumRecordFiller.ToString());



}
}
}
This is odd as I think the switch is right or do I just live with the nested if else?

Glenn

Answers (10)