Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a method in a Windows Form application that I'm making declared as:

C#
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

You need to write return code out of if condition.
Try below code:
C#
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;
        }
 
Share this answer
 
v3
Comments
suramrit 24-Jan-13 7:46am    
No, Doesnt solve it.
Vani Kulkarni 24-Jan-13 7:48am    
Does it give same error?
suramrit 24-Jan-13 7:49am    
Ya,perhaps I'm not declaring and returning the Color correctly?
suramrit 24-Jan-13 7:52am    
No, Sorry, this time the error is
Use of unassigned local variable 'clr'
Vani Kulkarni 24-Jan-13 7:55am    
Try:Color clr = new Color();
Then continue with your if condition.
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.
 
Share this answer
 
Comments
suramrit 24-Jan-13 7:52am    
Ya, got that... but the problem is now its showing
Error 3 Use of unassigned local variable 'clr'
Add return clr; before you end you method. so that at every case it will return something.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900