Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows C#4.0 WinForm , +
I have a method in a Windows Form application that I'm making declared as:
 
 
      public  Color get_color(double str)
        {
            Color clr;
           // int k = 0;
            if (Convert.ToDouble(str) < 25 && Convert.ToDouble(str) > 0)
            {
                // for (int j = 5; j < 11; j++)
                {
                    return clr = Color.FromArgb(255, 0, 0);
 
                }
 

            }
        }
 
While building the solution I'm having the error:
Error 3 "menu.Form2.get_color(double)': not all code paths return a value"
 
can someone please explain what is it that im doing wrong here, It'll be really helpful.
Posted 24-Jan-13 2:43am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You need to write return code out of if condition.
Try below code:
public  Color get_color(double str)
        {
            Color clr = new Color();
           // int k = 0;
            if (Convert.ToDouble(str) < 25 && Convert.ToDouble(str) > 0)
            {
                // for (int j = 5; j < 11; j++)
                {
                     clr = Color.FromArgb(255, 0, 0);
 
                }
 
            }
            return clr;
        }
  Permalink  
v3
Comments
suramrit at 24-Jan-13 7:46am
   
No, Doesnt solve it.
Vani Kulkarni at 24-Jan-13 7:48am
   
Does it give same error?
suramrit at 24-Jan-13 7:49am
   
Ya,perhaps I'm not declaring and returning the Color correctly?
suramrit at 24-Jan-13 7:52am
   
No, Sorry, this time the error is
Use of unassigned local variable 'clr'
Vani Kulkarni at 24-Jan-13 7:55am
   
Try:Color clr = new Color();
Then continue with your if condition.
suramrit at 24-Jan-13 7:58am
   
I've modified it to
 
public Color get_color(double str)
{
Color clr;
// int k = 0;
if (Convert.ToDouble(str) < 25 && Convert.ToDouble(str) > 0)
{
// for (int j = 5; j < 11; j++)

return clr = Color.FromArgb(255, 0, 0);
 


 
}
else return "clr"= Color.FromArgb(255,255,255);
}
 

but still for the clr in "" it says use of unassigned local variable
Vani Kulkarni at 24-Jan-13 8:02am
   
Updated my answer.
suramrit at 24-Jan-13 8:03am
   
even after making it color clr = new color();
I have the same error
Vani Kulkarni at 24-Jan-13 8:03am
   
I just tried it..works perfect for me. No error at all.
suramrit at 24-Jan-13 8:06am
   
Yes, it does work. Thanks a LOT.. You Rock..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

In the code you have written, you have returned from within the if condition.
 
But what if execution fails the if condition. In that case there is no return value.
As a result build fails.
 
Try putting a return condition for the else part as well.
  Permalink  
Comments
suramrit at 24-Jan-13 7:52am
   
Ya, got that... but the problem is now its showing
Error 3 Use of unassigned local variable 'clr'
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Add return clr; before you end you method. so that at every case it will return something.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 24 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100