don06van

Hi there

I've created a windows form in which the user inputs some data.

Anyways I was creating a method to create an ETA based on some of the data the user inputs in the Windows form.

so this is were I call the method in one of the buttonclick event handlers.

double tempETA = calculateETA(extensionType,tempWidth, tempHeight);

//and this is how i formed my method//

private double calculateETA(string extType, double Width, double Height)

{

double recievingTime;

double finalETA;

double numOfFeet;

if (extType == "pdf")

{

recievingTime = 6.00;

if (Width > Height)

{

if (Width < 41.5)

{

numOfFeet = Width / 12;

if (numOfFeet >= 3)

{

finalETA = recievingTime + 8.7;

return finalETA;

}

Now why am i getting that error

(there's a lot more of those if statements, a ton of them in fact, with no errors whatsoever)

If you someone could help me out that would be great




Re: Visual C# General "not all code paths return a value"

Ron.Whittle

There are not nearly enough closing '}' in your code that you posted. This makes it impossible to answer your question.






Re: Visual C# General "not all code paths return a value"

don06van

i didnt include my entire program

i know i have plenty of closing brackets

thats definently not the issue






Re: Visual C# General "not all code paths return a value"

CsharpNeophyte

We would need to see the whole routine to help with that particular error message.

I am assuming the compiler is generating the message for the calculateETA function.

Basically what it is saying that there is a path in your calculateETA function where the function can get to the final closing bracket for the function without ever hitting a "return" statement.

For instance (stupid example)

private double calc(int arg1)

{

if arg1 == 6

{

return 6.0

}

else

{

arg1++

}

}

In this case if arg1 is anything but 6 a "return" is never hit and the error is generated. There are two code paths the if and the else.

If you are getting that message as a runtime error I would assume that some combination of input values allows you to pass through your function without hitting a return.





Re: Visual C# General "not all code paths return a value"

mwalts

No, what the error means is that there are places you can go in that method that do not have return values, even though the method has a return type. Look through the method and see if there is any way of getting to the last curly brace without hitting a return statement. If there is, the message is correct and you need to add (at least) one return statement.

What the other poster was trying to tell you is that without seeing all the possible execution paths in this method, we really can't tell you where you want wrong.

Good luck,

-mwalts




Re: Visual C# General "not all code paths return a value"

Andreas Johansson

To get an exact answer you need to supply the code for the whole procedure.

When the code falls out of any of your checked cases you most likely does not return a value, that is most likely at the very end of your method.






Re: Visual C# General "not all code paths return a value"

Udhaya Kumar D

Hi,

I am answering this question by guessing that u would have done this mistake.

Suppose this your function:

private double calculateETA(string extType, double Width, double Height)

{

double recievingTime;

double finalETA;

double numOfFeet;


if (extType == "pdf")

{

recievingTime = 6.00;


if (Width > Height)

{

if (Width < 41.5)

{

numOfFeet = Width / 12;

if (numOfFeet >= 3)

{

finalETA = recievingTime + 8.7;

return finalETA;

}

}

}

}

}

If this ur code then it will definitely show that error because you are only returning once in the function and that to inside the "if" statement. Suppose if the condition for the "if" statement is not satisfied, then it will not enter into the "if" statement so nothing will be returned since u r returning only once that to in the if statement. So the compiler is showing the above error because you have promised the compiler that u will return a 'double' value.


private
double calculateETA(string extType, double Width, double Height)


Solution:

Change ur code as follows:

private double calculateETA(string extType, double Width, double Height)

{

double recievingTime;

double finalETA = 0.0;

double numOfFeet;


if (extType == "pdf")

{

recievingTime = 6.00;


if (Width > Height)

{

if (Width < 41.5)

{

numOfFeet = Width / 12;

if (numOfFeet >= 3)

{

finalETA = recievingTime + 8.7;


}

}

}

}


return finalETA;

}







Re: Visual C# General "not all code paths return a value"

don06van

Oh okay I get it now

I had a lot more if statements and some of them didnt have a return and so that was the problem.

thanks a lot

:-)