C# : Method Error - not all code paths return a value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • siri11
    New Member
    • Sep 2007
    • 38

    C# : Method Error - not all code paths return a value

    hi everyone!!!!

    I have written a function that shud return a decimal value.But I'm getting an error in c#.net(windows Application) which is a build error--->not all code paths return a value.Can anyone plzz suggest why this error is popping up.What does this mean???

    Thnks in advance
    siri
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    Originally posted by siri11
    hi everyone!!!!

    I have written a function that shud return a decimal value.But I'm getting an error in c#.net(windows Application) which is a build error--->not all code paths return a value.Can anyone plzz suggest why this error is popping up.What does this mean???

    Thnks in advance
    siri
    do you have a return statement?

    make sure you have one return statement in the method

    the followinf method will give the error you are reciving
    [CODE=cpp]private decimal myMethod()
    {
    decimal i,j;
    i = 10;
    j = 20;
    if(i + j > 100)
    {
    return i + j;
    }
    }[/CODE]

    You should be doing something similar to
    [CODE=cpp]private decimal myMethod()
    {
    decimal i,j;
    i = 10;
    j = 20;
    if(i + j > 100)
    {
    return i + j;
    }
    return 0;
    }[/CODE]

    returning values in between is something which i consider to be not so good programming at times (though i still use it because it saves time, along with some other factors)

    the suggested implementation whould be
    [CODE=cpp]private decimal myMethod()
    {
    decimal i,j;
    i = 10;
    j = 20;
    decimal result = i + j;
    if(result > 100)
    {
    //do nothing
    }
    else
    result = 0;
    return result;
    }[/CODE]

    Comment

    • dip_developer
      Recognized Expert Contributor
      • Aug 2006
      • 648

      #3
      Originally posted by siri11
      hi everyone!!!!

      I have written a function that shud return a decimal value.But I'm getting an error in c#.net(windows Application) which is a build error--->not all code paths return a value.Can anyone plzz suggest why this error is popping up.What does this mean???

      Thnks in advance
      siri
      you have something wrong in the return statement of your function.....yo ur function must have to return something in all possible combination of execution...... ..suppose you have some case statements..... all the case statements should return a value after execution...... .check for those.......

      you may submit your code snippet to get help.......

      Comment

      • Shashi Sadasivan
        Recognized Expert Top Contributor
        • Aug 2007
        • 1435

        #4
        Originally posted by dip_developer
        suppose you have some case statements..... all the case statements should return a value after execution...... .
        I doubt that !!!

        after the switch case statement finishes at the end there should be a return value. Some case statments might return, but it is not necessary.

        Comment

        • dip_developer
          Recognized Expert Contributor
          • Aug 2006
          • 648

          #5
          Originally posted by Shashi Sadasivan
          I doubt that !!!

          after the switch case statement finishes at the end there should be a return value. Some case statments might return, but it is not necessary.
          sorry friend.......no t all...........

          thanks for your correction..... I apologise...... ..

          Comment

          • karthickbabu
            New Member
            • Sep 2007
            • 33

            #6
            Using Try Catch Block to rectify this problem

            But in Catch you wont catch and display the exception message. In Catch you throw the exception. I too got this warning i think its not error, am i right. In Function we return some values thats why we wont catch the exception.

            wrtie this code
            try

            your code

            catch
            Throw ex // important

            end try

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by Shashi Sadasivan
              ...

              returning values in between is something which i consider to be not so good programming at times (though i still use it because it saves time, along with some other factors)

              the suggested implementation whould be
              [CODE=cpp]private decimal myMethod()
              {
              decimal i,j;
              i = 10;
              j = 20;
              decimal result = i + j;
              if(result > 100)
              {
              //do nothing
              }
              else
              result = 0;
              return result;
              }[/CODE]
              If you are writing a search method that goes through a list then you want to return (or break) as soon as you find the object you are searching for rather than letting the loop run through to the end of the list. Putting return statements elsewhere besides the last line in a method is not bad programming practice at all.

              Comment

              • Shashi Sadasivan
                Recognized Expert Top Contributor
                • Aug 2007
                • 1435

                #8
                Originally posted by r035198x
                If you are writing a search method that goes through a list then you want to return (or break) as soon as you find the object you are searching for rather than letting the loop run through to the end of the list. Putting return statements elsewhere besides the last line in a method is not bad programming practice at all.
                Hi r0
                Well..i use the break statements as soon as i find the element i need.
                Its just that, there was time where i had to modify code very often (talk about clients who follow XP methodology and no docos) the intermediate return statements created almost spagetti code!

                But i still use it sometimes....
                it feels better to see one return statement in a method (but then there could be a lot of perspectives to this)

                Comment

                Working...