Click here to Skip to main content
15,891,184 members
Home / Discussions / C#
   

C#

 
QuestionNot all code paths return a value Pin
Nikhil Bhivgade25-Jun-10 1:50
professionalNikhil Bhivgade25-Jun-10 1:50 
AnswerRe: Not all code paths return a value PinPopular
harold aptroot25-Jun-10 1:57
harold aptroot25-Jun-10 1:57 
AnswerRe: Not all code paths return a value Pin
riced25-Jun-10 1:58
riced25-Jun-10 1:58 
GeneralRe: Not all code paths return a value Pin
Pete O'Hanlon25-Jun-10 2:04
mvePete O'Hanlon25-Jun-10 2:04 
GeneralRe: Not all code paths return a value Pin
riced25-Jun-10 2:15
riced25-Jun-10 2:15 
GeneralRe: Not all code paths return a value Pin
Anthony Mushrow25-Jun-10 13:13
professionalAnthony Mushrow25-Jun-10 13:13 
GeneralRe: Not all code paths return a value Pin
Pete O'Hanlon25-Jun-10 13:23
mvePete O'Hanlon25-Jun-10 13:23 
AnswerRe: Not all code paths return a value Pin
Pete O'Hanlon25-Jun-10 2:03
mvePete O'Hanlon25-Jun-10 2:03 
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



AnswerRe: Not all code paths return a value Pin
OriginalGriff25-Jun-10 2:48
mveOriginalGriff25-Jun-10 2:48 
GeneralRe: Not all code paths return a value Pin
freakyit25-Jun-10 3:14
freakyit25-Jun-10 3:14 
GeneralRe: Not all code paths return a value Pin
OriginalGriff25-Jun-10 3:26
mveOriginalGriff25-Jun-10 3:26 
GeneralRe: Not all code paths return a value Pin
Luc Pattyn25-Jun-10 3:39
sitebuilderLuc Pattyn25-Jun-10 3:39 
GeneralRe: Not all code paths return a value Pin
Pete O'Hanlon25-Jun-10 3:45
mvePete O'Hanlon25-Jun-10 3:45 
GeneralRe: Not all code paths return a value Pin
Luc Pattyn25-Jun-10 4:06
sitebuilderLuc Pattyn25-Jun-10 4:06 
GeneralRe: Not all code paths return a value Pin
harold aptroot25-Jun-10 3:18
harold aptroot25-Jun-10 3:18 
GeneralRe: Not all code paths return a value Pin
freakyit25-Jun-10 3:26
freakyit25-Jun-10 3:26 
AnswerRe: Not all code paths return a value Pin
harold aptroot26-Jun-10 0:56
harold aptroot26-Jun-10 0:56 
AnswerRe: Not all code paths return a value Pin
yu-jian26-Jun-10 7:47
yu-jian26-Jun-10 7:47 
Questionwhat to do after a read from a socket Timeout Pin
manustone25-Jun-10 1:38
manustone25-Jun-10 1:38 
AnswerRe: what to do after a read from a socket Timeout Pin
David Knechtges25-Jun-10 3:19
David Knechtges25-Jun-10 3:19 
AnswerRe: what to do after a read from a socket Timeout Pin
freakyit25-Jun-10 3:21
freakyit25-Jun-10 3:21 
GeneralRe: what to do after a read from a socket Timeout Pin
manustone29-Jun-10 23:16
manustone29-Jun-10 23:16 
AnswerRe: what to do after a read from a socket Timeout Pin
Ennis Ray Lynch, Jr.25-Jun-10 4:15
Ennis Ray Lynch, Jr.25-Jun-10 4:15 
GeneralRe: what to do after a read from a socket Timeout Pin
manustone29-Jun-10 23:16
manustone29-Jun-10 23:16 
QuestionCollection of unique ids Pin
Chiman125-Jun-10 0:38
Chiman125-Jun-10 0:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.