Catching - "not all code paths return a value"

Hi, In converting over to C# from VB I notice that I start to get a few messages on my functions saying that not all code paths return a value.
Now I believe that this is due to some levels of a conditional statement not having a return value (is that a correct assumption?).
In more complicated conditional staements are there any ways in ASP.NET to identify which code paths are producing this error. or a simple method to resolve this.
Simon CrippsOwnerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
Now I believe that this is due to some levels of a  conditional statement not having a return value (is that a correct  assumption?).

Yes - it is a return without supplying a value

Simple method to resolve - none that I know of.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Éric MoreauSenior .Net ConsultantCommented:
you surely have IF statements without a ELSE statement.
or you have SELECT CASE (SWITCH) statements without the ELSE counterpart.
0
lazyberezovskyCommented:
AFAIK VB allows not to assign return value to functions - default value will be used in this case. Maybe when you are converting your code those execution passes resulted in such case. If so, you can fix your logic by adding return of default value to the end of function. E.g. false for bool and 0 for int.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

ZippitCommented:
You must be using the 'return' keyword to cause a procedure to 'return' from inside a conditional.  So make sure all your returns are actually returning a value (not just a blank return to exit the function).  Other then that, put a default 'return' at the end of your proc to catch all other cases.
0
ToddBeaulieuCommented:
This is a funamental clean coding issue. What would you rather debug, a routine that has 27 seperate exit points, or a single exit point? What if you needed to add a line of code that gets executed in addition to each of those exit points? You wouldn't want to seek out all exit points, inserting duplicate code into each.

I understand that you inherited this, but consider cleaning up any code that takes more than a a minute to figure out what the heck it's doing.

Consider creating a local return variable, possibly with a default value.

Then, in your conditional code, rather than exiting, you would assign values to the return variable.

Finally, there would be a single exit point at the end of the method.

This philosophy goes back to the stone ages, and continues to be a sound one.
0
lazyberezovskyCommented:
ToddBeaulieu,
Single exit point is an obsolete style of programming. Passing result value over all method body (knowing that it will not be changed any more) makes code less readable.
You better to read here http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement

BTW Martin Fowler prefers guard conditions for exiting method.
0
ToddBeaulieuCommented:
Oh boy, I'm not getting sucked into that debate!
0
AndyAinscowFreelance programmer / ConsultantCommented:
Personally I prefer to explicitly state the return value where needed - readability - with a default value at the end of the function.

eg.
int Foo()
{
  if(x)
  {
 return 1;
  }
  else if(y)
{
 return 2;
}

return 0;  //no matches found.
}


As I initially said somewhere you have a missing return, it is likely to be something like

int Foo()
 {
   if(x)
   {
  return 1;
   }
   else if(y)
 {
  return 2;
 }
 
 //function exits WITHOUT a return value being specified
 }

0
ZippitCommented:
Hahaha.  Couldn't agree more ToddBeaulieu
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.