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. General Programming
  3. C#
  4. Not all code paths return a value

Not all code paths return a value

Scheduled Pinned Locked Moved C#
help
18 Posts 9 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.
  • N Offline
    N Offline
    Nikhil Bhivgade
    wrote on last edited by
    #1

    public Boolean valid()
    {
    for (int i=0; i < cmbSampleName.Items.Count; i++)
    {

                if (txtSampleName.Text == cmbSampleName.Items\[i\].ToString())
                {
                    return true;
                    break;
                }
                else
                {
                    return false;
                    
                }
            }
    
        }
    

    i want to check weather the current text in textbox is present in combobox or not plz help

    L R P OriginalGriffO Y 6 Replies Last reply
    0
    • N Nikhil Bhivgade

      public Boolean valid()
      {
      for (int i=0; i < cmbSampleName.Items.Count; i++)
      {

                  if (txtSampleName.Text == cmbSampleName.Items\[i\].ToString())
                  {
                      return true;
                      break;
                  }
                  else
                  {
                      return false;
                      
                  }
              }
      
          }
      

      i want to check weather the current text in textbox is present in combobox or not plz help

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Your code makes no sense, you're only checking the first item. Also, you're not returning anything if there are no items in the combobox, which is why you have a compile error. Maybe this is what you wanted? (warning: untested!)

      public bool IsValid()
      {
      for (int i = 0; i < cmbSampleName.Items.Count; i++)
      {
      if (txtSampleName.Text == cmbSampleName.Items[i].ToString())
      return true;
      }
      return false;
      }

      1 Reply Last reply
      0
      • N Nikhil Bhivgade

        public Boolean valid()
        {
        for (int i=0; i < cmbSampleName.Items.Count; i++)
        {

                    if (txtSampleName.Text == cmbSampleName.Items\[i\].ToString())
                    {
                        return true;
                        break;
                    }
                    else
                    {
                        return false;
                        
                    }
                }
        
            }
        

        i want to check weather the current text in textbox is present in combobox or not plz help

        R Offline
        R Offline
        riced
        wrote on last edited by
        #3

        I suggest stepping through it with the debugger and paying attention to what it does. If you cannot do that, then trace through the code manually setting i to 0 and considering both cases where if is true and where it is false. Then ask what happens if i is 1 - can it get there?. Here's a clue - it will not reach the break line.

        Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.

        P 1 Reply Last reply
        0
        • N Nikhil Bhivgade

          public Boolean valid()
          {
          for (int i=0; i < cmbSampleName.Items.Count; i++)
          {

                      if (txtSampleName.Text == cmbSampleName.Items\[i\].ToString())
                      {
                          return true;
                          break;
                      }
                      else
                      {
                          return false;
                          
                      }
                  }
          
              }
          

          i want to check weather the current text in textbox is present in combobox or not plz help

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          You may thing that you've got them all covered, but there's a big condition that you haven't checked. What happens if cmbSampleName.Items.Count is 0? The for loop won't execute, so a code path is causing you a problem. What you could do, is rewrite this by removing return false and place it outside the scope of the for loop (i.e. before the final }). This turns your method into:

          public bool Valid()
          {
          for (int i=0; i < cmbSampleName.Items.Count; i++)
          {
          if (txtSampleName.Text == cmbSampleName.Items[i].ToString())
          return true; // The break in the original sample is superflous here
          // as it would never be reached.
          }
          return false;
          }

          Alternatively, you could use the Find method on cmbSampleName.Items to find out if the name is present or not (if it returns null, it's not present).

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

          1 Reply Last reply
          0
          • R riced

            I suggest stepping through it with the debugger and paying attention to what it does. If you cannot do that, then trace through the code manually setting i to 0 and considering both cases where if is true and where it is false. Then ask what happens if i is 1 - can it get there?. Here's a clue - it will not reach the break line.

            Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            He won't be able to step through it as the code won't compile.

            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

            My blog | My articles | MoXAML PowerToys | Onyx

            R 1 Reply Last reply
            0
            • P Pete OHanlon

              He won't be able to step through it as the code won't compile.

              "WPF has many lovers. It's a veritable porn star!" - Josh Smith

              As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

              My blog | My articles | MoXAML PowerToys | Onyx

              R Offline
              R Offline
              riced
              wrote on last edited by
              #6

              That's why I suggested a manual trace - but do people get taught how to that these days? :)

              Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.

              A 1 Reply Last reply
              0
              • N Nikhil Bhivgade

                public Boolean valid()
                {
                for (int i=0; i < cmbSampleName.Items.Count; i++)
                {

                            if (txtSampleName.Text == cmbSampleName.Items\[i\].ToString())
                            {
                                return true;
                                break;
                            }
                            else
                            {
                                return false;
                                
                            }
                        }
                
                    }
                

                i want to check weather the current text in textbox is present in combobox or not plz help

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #7

                Just to add the above:

                Member 4081808 wrote:

                return true; break;

                Won't compile either: unreachable code - the "break" will never be executed as the method returns before it.

                Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                F L 2 Replies Last reply
                0
                • OriginalGriffO OriginalGriff

                  Just to add the above:

                  Member 4081808 wrote:

                  return true; break;

                  Won't compile either: unreachable code - the "break" will never be executed as the method returns before it.

                  Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.

                  F Offline
                  F Offline
                  freakyit
                  wrote on last edited by
                  #8

                  lol thats wrong -> compile would succeed.. but bring up a warining as you described :D xyz :)

                  OriginalGriffO 1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Just to add the above:

                    Member 4081808 wrote:

                    return true; break;

                    Won't compile either: unreachable code - the "break" will never be executed as the method returns before it.

                    Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    This aint Java :)

                    F 1 Reply Last reply
                    0
                    • L Lost User

                      This aint Java :)

                      F Offline
                      F Offline
                      freakyit
                      wrote on last edited by
                      #10

                      hmm i use #develop :) with C# xD

                      1 Reply Last reply
                      0
                      • F freakyit

                        lol thats wrong -> compile would succeed.. but bring up a warining as you described :D xyz :)

                        OriginalGriffO Offline
                        OriginalGriffO Offline
                        OriginalGriff
                        wrote on last edited by
                        #11

                        You mean you don't have "treat all warnings as errors" enabled? :omg: :laugh:

                        Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                        L P 2 Replies Last reply
                        0
                        • OriginalGriffO OriginalGriff

                          You mean you don't have "treat all warnings as errors" enabled? :omg: :laugh:

                          Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #12

                          :thumbsup:

                          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                          Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

                          1 Reply Last reply
                          0
                          • OriginalGriffO OriginalGriff

                            You mean you don't have "treat all warnings as errors" enabled? :omg: :laugh:

                            Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.

                            P Offline
                            P Offline
                            Pete OHanlon
                            wrote on last edited by
                            #13

                            No. I develop in WPF where warnings are deferred until runtime.

                            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                            My blog | My articles | MoXAML PowerToys | Onyx

                            L 1 Reply Last reply
                            0
                            • P Pete OHanlon

                              No. I develop in WPF where warnings are deferred until runtime.

                              "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                              As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                              My blog | My articles | MoXAML PowerToys | Onyx

                              L Offline
                              L Offline
                              Luc Pattyn
                              wrote on last edited by
                              #14

                              Have you tried adding

                              <option strict="on">

                              ? :)

                              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                              Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

                              1 Reply Last reply
                              0
                              • R riced

                                That's why I suggested a manual trace - but do people get taught how to that these days? :)

                                Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.

                                A Offline
                                A Offline
                                Anthony Mushrow
                                wrote on last edited by
                                #15

                                riced wrote:

                                but do people get taught how to that these days?

                                Well the course I did tried to. Of course they went to the other extreme and got us to design the application fully on paper and then set up test and run through manually and document the results, only once we had proved that the design worked as expected could we actually program it. I suppose it's good practice for simple programs (which was the case), to grasp the concept. But for most things it's usually quicker to design a more rough idea of what should be happening, code it and then debug the little problems. So in short, yes people do get taught that these days.

                                My current favourite quote is: Punch them in the face, see what happens!

                                -SK Genius

                                P 1 Reply Last reply
                                0
                                • A Anthony Mushrow

                                  riced wrote:

                                  but do people get taught how to that these days?

                                  Well the course I did tried to. Of course they went to the other extreme and got us to design the application fully on paper and then set up test and run through manually and document the results, only once we had proved that the design worked as expected could we actually program it. I suppose it's good practice for simple programs (which was the case), to grasp the concept. But for most things it's usually quicker to design a more rough idea of what should be happening, code it and then debug the little problems. So in short, yes people do get taught that these days.

                                  My current favourite quote is: Punch them in the face, see what happens!

                                  -SK Genius

                                  P Offline
                                  P Offline
                                  Pete OHanlon
                                  wrote on last edited by
                                  #16

                                  SK Genius wrote:

                                  it's usually quicker to design a more rough idea of what should be happening, code it and then debug the little problems

                                  Isn't this the essence of Test Driven Development?

                                  "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                                  As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                                  My blog | My articles | MoXAML PowerToys | Onyx

                                  1 Reply Last reply
                                  0
                                  • N Nikhil Bhivgade

                                    public Boolean valid()
                                    {
                                    for (int i=0; i < cmbSampleName.Items.Count; i++)
                                    {

                                                if (txtSampleName.Text == cmbSampleName.Items\[i\].ToString())
                                                {
                                                    return true;
                                                    break;
                                                }
                                                else
                                                {
                                                    return false;
                                                    
                                                }
                                            }
                                    
                                        }
                                    

                                    i want to check weather the current text in textbox is present in combobox or not plz help

                                    L Offline
                                    L Offline
                                    Lost User
                                    wrote on last edited by
                                    #17

                                    I have been thinking about how it would make sense to write code like this. After all, you wouldn't have written it like this if you had no reason to do so. I think I figured it out - did you think that return value; only sets the value that will be returned, instead of immediately* returning that value? It all makes sense then - as long as they are not equal, you would keep the return value on false; and when they are equal you'd set the return value to true and exit the method. The problem with that is, of course, that return doesn't "set the value that will be returned", it immediately* exits the method (and it also returns the specified value, of course) *: except in the presence of try/finally

                                    1 Reply Last reply
                                    0
                                    • N Nikhil Bhivgade

                                      public Boolean valid()
                                      {
                                      for (int i=0; i < cmbSampleName.Items.Count; i++)
                                      {

                                                  if (txtSampleName.Text == cmbSampleName.Items\[i\].ToString())
                                                  {
                                                      return true;
                                                      break;
                                                  }
                                                  else
                                                  {
                                                      return false;
                                                      
                                                  }
                                              }
                                      
                                          }
                                      

                                      i want to check weather the current text in textbox is present in combobox or not plz help

                                      Y Offline
                                      Y Offline
                                      yu jian
                                      wrote on last edited by
                                      #18

                                      After the for loop you should return a value, because perhaps the for loop is not executed

                                      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