1
Answer

Trouble with case/switch statent

simon taylor

simon taylor

13y
12k
1

Hi,
    i am having problems trying to validate my form controlls which are all text boxes. I am using a switch statment to testing things like if the fields are blank ect. However, when i run my app and place an @ sign in my postcode field for example, with all the other text boxes left blank. It shows my messegboxes correctly up to the point at with it sees the @ sign. Instead it displays my user generated message of: " Job center fied cannot be blank". However, i have a check for the @ sign . Why does it not display the correct message when it sees an @ sign?
 
private void cmdAdd_Click(object sender, EventArgs e)

  {

  

   JobCenter jCenter = new JobCenter();

   string jobCenterName;

   string jobCenterAdd;

  



  

   jCenter.set_jobCenterName(txtJobCenterName.Text) ;

   jobCenterName = jCenter.get_jobCenterName(); 

   switch (jobCenterName)

   {

    case "":

     MessageBox.Show("Job Center Name cannot be blank");

     this.allClear();

     break;


case "@":

     MessageBox.Show("Job Center Name field cannot contain special characters e.g @ ");

     this.allClear();

     break;



    case "+": // does not seemt to validate

     MessageBox.Show("Job Center Name will not take values such as positive + sign");

     this.allClear();

     break;



    case "-":

     MessageBox.Show("Job Center Name will not take minus values");

     this.allClear();

     break;

    

    case "!":

     MessageBox.Show("Job Center Name field will not accept specail characters such as exclamtion mark");

     this.allClear();

     break;



    default:



     return;

 

   }



string jobPostCd;

   jCenter.set_jobCenterPostCode(txtJobCenterPostCode.Text);

   jobPostCd = jCenter.get_jobCenterPostCode();

   switch (jobPostCd)

   {

    case "":

     MessageBox.Show("Job Center Post Code cannot be blank");

     this.allClear();

     break;



    case "@":

     MessageBox.Show("Job Center Post Code field cannot contain special characters e.g @ ");

     this.allClear();

     break;



    case "+":

     MessageBox.Show("Job Center Post Code will not take values such as positive + sign");

     this.allClear();

     break;



    case "-":

     MessageBox.Show("Job Center Post Code will not take minus values");

     this.allClear();

     break;



    case "!":

     MessageBox.Show("Job Center Post Code field will not accept specail characters such as exclamtion mark");

     this.allClear();

     break;



    default:



     return;



   }

I have skipped the other case stements because they are doing the same thing.
Answers (1)