exit after if statement
-
my application works like this,i have a datagrid,a dropdownlist and a submit button,my dropdownlist contains a list of intervals eg(100-999)(2100-2199),in order for the user to add to the grid he has to choose an interval from the dropdown,and the id for the row he's adding has to be in that particular interval. i have a method/function that checks if the id is in the chosen interval private void checkvalue(int value) { switch(DropdownList1.SelectedIndex) { case 0: if(value>=100 && value<=999) { } else { Label1.Text="id not in required interval" } break; case 1: if(value>=1000 && value<=1999) { } else { Label1.Text="id not in required interval" } break; } and the button clickevent private void ButtSubmit_Click(object sender, System.EventArgs e) { checkvalue(num); //adding to the grid collection.add(num); ... } when i click my submit button ,and into the checkvalue callfunction,if the value is not in the required interval ,it prints the text message but still continues to add the row to my grid i want it to stop after it prints the message and exit without adding the row to my grid,what do you suggest i do.i hope this is clear enough thanks regards paula
-
my application works like this,i have a datagrid,a dropdownlist and a submit button,my dropdownlist contains a list of intervals eg(100-999)(2100-2199),in order for the user to add to the grid he has to choose an interval from the dropdown,and the id for the row he's adding has to be in that particular interval. i have a method/function that checks if the id is in the chosen interval private void checkvalue(int value) { switch(DropdownList1.SelectedIndex) { case 0: if(value>=100 && value<=999) { } else { Label1.Text="id not in required interval" } break; case 1: if(value>=1000 && value<=1999) { } else { Label1.Text="id not in required interval" } break; } and the button clickevent private void ButtSubmit_Click(object sender, System.EventArgs e) { checkvalue(num); //adding to the grid collection.add(num); ... } when i click my submit button ,and into the checkvalue callfunction,if the value is not in the required interval ,it prints the text message but still continues to add the row to my grid i want it to stop after it prints the message and exit without adding the row to my grid,what do you suggest i do.i hope this is clear enough thanks regards paula
- Move collection.add(num); into the method e.g. if(value>=1000 && value<=1999) { collection.add(num); } and take it out of the button click event 2) private bool checkvalue(int value) { if(value>=1000 && value<=1999) { return true; } else { Label1.Text="id not in required interval" return false; } } then in the button event if(checkvalue(num)) collection.add(num);
-
my application works like this,i have a datagrid,a dropdownlist and a submit button,my dropdownlist contains a list of intervals eg(100-999)(2100-2199),in order for the user to add to the grid he has to choose an interval from the dropdown,and the id for the row he's adding has to be in that particular interval. i have a method/function that checks if the id is in the chosen interval private void checkvalue(int value) { switch(DropdownList1.SelectedIndex) { case 0: if(value>=100 && value<=999) { } else { Label1.Text="id not in required interval" } break; case 1: if(value>=1000 && value<=1999) { } else { Label1.Text="id not in required interval" } break; } and the button clickevent private void ButtSubmit_Click(object sender, System.EventArgs e) { checkvalue(num); //adding to the grid collection.add(num); ... } when i click my submit button ,and into the checkvalue callfunction,if the value is not in the required interval ,it prints the text message but still continues to add the row to my grid i want it to stop after it prints the message and exit without adding the row to my grid,what do you suggest i do.i hope this is clear enough thanks regards paula
Sorry now that i have posted and reading my message I see that it isnt so clear as it was in my head, basically I have given you two options here - dont do both just do 1) or just do 2)
-
- Move collection.add(num); into the method e.g. if(value>=1000 && value<=1999) { collection.add(num); } and take it out of the button click event 2) private bool checkvalue(int value) { if(value>=1000 && value<=1999) { return true; } else { Label1.Text="id not in required interval" return false; } } then in the button event if(checkvalue(num)) collection.add(num);
No need to do that. Just have checkNum() return a boolean value (true if ok, false otherwise) and add only in case of success.
-
No need to do that. Just have checkNum() return a boolean value (true if ok, false otherwise) and add only in case of success.
Thats what I said in point 2) - I realized after my post that it didn't really make a lot of sense in writting! I should of first said here are your option!
-
- Move collection.add(num); into the method e.g. if(value>=1000 && value<=1999) { collection.add(num); } and take it out of the button click event 2) private bool checkvalue(int value) { if(value>=1000 && value<=1999) { return true; } else { Label1.Text="id not in required interval" return false; } } then in the button event if(checkvalue(num)) collection.add(num);
in order for the user to add to the grid he has to choose an interval from the dropdownlist,and the id for the row he's adding has to be in that particular interval,so i modified my code to private bool CheckDatakey(int key) { switch(WebCombo1.SelectedIndex) { case 0: if(key>=100 && key<=999) { return true; } else { return false; } break; case 1: if(key>=2100 && key<=2199) { return true; } else { return false; } break; } my submit button: private void ButtSubmit_Click(object sender, System.EventArgs e) { if(checkvalue(num)==true) { collection.add(num); ... } else { label.text ="value is not in the required interval" } } i have done those changes to my code,im getting this error message "UPd.WebForm1.CheckDatakey(int )': not all code paths return a value" regards paula
-
in order for the user to add to the grid he has to choose an interval from the dropdownlist,and the id for the row he's adding has to be in that particular interval,so i modified my code to private bool CheckDatakey(int key) { switch(WebCombo1.SelectedIndex) { case 0: if(key>=100 && key<=999) { return true; } else { return false; } break; case 1: if(key>=2100 && key<=2199) { return true; } else { return false; } break; } my submit button: private void ButtSubmit_Click(object sender, System.EventArgs e) { if(checkvalue(num)==true) { collection.add(num); ... } else { label.text ="value is not in the required interval" } } i have done those changes to my code,im getting this error message "UPd.WebForm1.CheckDatakey(int )': not all code paths return a value" regards paula
this happens because in certain cases your method can end up not returning anything at all(if it doesnt find a match in the switch statement) and it has to return a bool so take out the breaks (that will cause another error - unreachable code) and add return true (or false) right at the very end of the method -
private bool CheckDatakey(int key) { switch(WebCombo1.SelectedIndex) { case 0: if(key>=100 && key<=999) { return true; } else { return false; } case 1: if(key>=2100 && key<=2199) { return true; } else { return false; } } return true; }
-
this happens because in certain cases your method can end up not returning anything at all(if it doesnt find a match in the switch statement) and it has to return a bool so take out the breaks (that will cause another error - unreachable code) and add return true (or false) right at the very end of the method -
private bool CheckDatakey(int key) { switch(WebCombo1.SelectedIndex) { case 0: if(key>=100 && key<=999) { return true; } else { return false; } case 1: if(key>=2100 && key<=2199) { return true; } else { return false; } } return true; }