Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. exit after if statement

exit after if statement

Scheduled Pinned Locked Moved ASP.NET
css
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PaulaM
    wrote on last edited by
    #1

    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

    G 2 Replies Last reply
    0
    • P PaulaM

      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

      G Offline
      G Offline
      Gavin Jeffrey
      wrote on last edited by
      #2
      1. 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);
      P P 2 Replies Last reply
      0
      • P PaulaM

        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

        G Offline
        G Offline
        Gavin Jeffrey
        wrote on last edited by
        #3

        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)

        1 Reply Last reply
        0
        • G Gavin Jeffrey
          1. 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);
          P Offline
          P Offline
          Plunging_Falcon
          wrote on last edited by
          #4

          No need to do that. Just have checkNum() return a boolean value (true if ok, false otherwise) and add only in case of success.

          G 1 Reply Last reply
          0
          • P Plunging_Falcon

            No need to do that. Just have checkNum() return a boolean value (true if ok, false otherwise) and add only in case of success.

            G Offline
            G Offline
            Gavin Jeffrey
            wrote on last edited by
            #5

            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!

            1 Reply Last reply
            0
            • G Gavin Jeffrey
              1. 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);
              P Offline
              P Offline
              PaulaM
              wrote on last edited by
              #6

              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

              G 1 Reply Last reply
              0
              • P PaulaM

                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

                G Offline
                G Offline
                Gavin Jeffrey
                wrote on last edited by
                #7

                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; }

                P 1 Reply Last reply
                0
                • G Gavin Jeffrey

                  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; }

                  P Offline
                  P Offline
                  PaulaM
                  wrote on last edited by
                  #8

                  its working now thank you:) regards paula

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups