473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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

38
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
Nov 20 '07 #1
7 10790
Shashi Sadasivan
1,435 Expert 1GB
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
Expand|Select|Wrap|Line Numbers
  1. private decimal myMethod()
  2. {
  3.    decimal i,j;
  4.    i = 10;
  5.    j = 20;
  6.    if(i + j > 100)
  7.    {
  8.       return i + j;
  9.    }
  10. }
You should be doing something similar to
Expand|Select|Wrap|Line Numbers
  1. private decimal myMethod()
  2. {
  3.    decimal i,j;
  4.    i = 10;
  5.    j = 20;
  6.    if(i + j > 100)
  7.    {
  8.       return i + j;
  9.    }
  10.    return 0;
  11. }
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
Expand|Select|Wrap|Line Numbers
  1. private decimal myMethod()
  2. {
  3.    decimal i,j;
  4.    i = 10;
  5.    j = 20;
  6.    decimal result = i + j;
  7.    if(result > 100)
  8.    {
  9.       //do nothing
  10.    }
  11.    else
  12.      result = 0;
  13.    return result;
  14. }
Nov 20 '07 #2
dip_developer
648 Expert 512MB
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.....your 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.......
Nov 20 '07 #3
Shashi Sadasivan
1,435 Expert 1GB
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.
Nov 20 '07 #4
dip_developer
648 Expert 512MB
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.......not all...........

thanks for your correction.....I apologise........
Nov 20 '07 #5
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
Dec 1 '07 #6
r035198x
13,262 8TB
...

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
Expand|Select|Wrap|Line Numbers
  1. private decimal myMethod()
  2. {
  3.    decimal i,j;
  4.    i = 10;
  5.    j = 20;
  6.    decimal result = i + j;
  7.    if(result > 100)
  8.    {
  9.       //do nothing
  10.    }
  11.    else
  12.      result = 0;
  13.    return result;
  14. }
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.
Dec 1 '07 #7
Shashi Sadasivan
1,435 Expert 1GB
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)
Dec 2 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Tony Wright | last post by:
Hi, I am having a problem installing an msi for a web site. The error message I am getting is: "The specified path 'http://mipdev05/features/Fas2' is unavailable. The Internet Information...
12
by: Jose Fernandez | last post by:
Hello. I'm building a web service and I get this error. NEWS.News.CoverNews(string)': not all code paths return a value This is the WebMethod public SqlDataReader CoverNews(string Sport)...
4
by: OutdoorGuy | last post by:
Greetings, I am attempting to compile the code below, but I am receiving an error message when I do so. The error message is: "CSO161: 'Forloop.CalcAvg(int)': Not all code paths return a...
3
by: Oberon | last post by:
How do I deal with this? I am getting an error for each get in the Game class (see code below). In the simplified example below I have reduced this to just 3 fields, one which can be NULL. I...
7
by: p | last post by:
WE had a Crystal 8 WebApp using vs 2002 which we upgraded to VS2003. I also have Crystal 9 pro on my development machine. The web app runs fine on my dev machine but am having problems deploying....
12
by: Ant | last post by:
Hi, I need to return a value to a methods call from a switch statement nested in the method. How can this be done? int myMethod(int someValue) { switch (someValue) {
9
by: Doug | last post by:
Hi I have a method that I would like to return the value to the calling event. I have a bad example below that doesnt give me what I want. What I want is to have a my readxml mthod return the...
1
by: dba | last post by:
Hello All, And thanks in advance for taking the time to read this. I was given an interface definition to a COM object. Now I'd like to be able to compile the interface into it's own assembly...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.