Dan
Dan

Reputation: 195

return value from within loop not showing

Morning, simple stupid question. I have found post with similar issues but after reading through it does not solve my error.

Return value from For loop

Can't get a return value from a foreach loop inside a method

The methods: meth1 meth2 ect....all return a value but at the moment I am getting the error

"Error 1 'Proj5.Program.meth1(int)': not all code paths return a value" for each meth method.

My logical guess is its not seeing the value within the loop?? ...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Proj5
{
class Program
{
    static void Main()
    {
        for (int i = 1; i < 101; i++)
        {
            if (i == 3 || 0 == (i % 3) || 0 == (i % 5) || i == 5)
            {
                Console.Write(meth1(i));
                Console.Write(meth2(i));
                Console.Write(meth3(i));
                Console.Write(meth4(i));
            }
            else
            {
                Console.Write(TheInt(i));
            }
        }
        Console.ReadLine();
    }

    static string meth1(int i)
    {
        string f = "fuzz";

        if (i == 3)
        {
            return f;
        }
    }
    static string meth2(int i)
    {
        string f = "fuzz";

        if (0 == (i % 3))
        {
            return f;
        }
    }
    static string meth3(int i)
    {
        string b = "buzz";

        if (i == 5)
        {
            return b;
        }

    }
    static string meth4(int i)
    {
        string b = "buzz";

        if (0 == (i % 5))
        {
            return b;
        }
    }
    static int TheInt(int i)
    {
        return i;
    }

}
}

Upvotes: 0

Views: 1344

Answers (3)

Rapha&#235;l Althaus
Rapha&#235;l Althaus

Reputation: 60503

You say your method should return a string, but if i<>3, you don't say what should be returned. Method 2 and 3 have the same problem, by the way (and 4 also). I won't speak about method TheInt, which is... funny ;)

Correction

static string meth1(int i)
    {
        string f = "fuzz";

        if (i == 3)
        {
            return f;
        }
        return null;//add a "default" return, null or string.Empty
    }

or shorter

static string meth1(int i) {
    return (i == 3) ? "fuzz" : null/*or string.Empty*/;
}

Upvotes: 3

Steve
Steve

Reputation: 216313

When you declare a method as returning a values (as for meth1, etc) you should honor this declaration.

Your method doesn't return anything if the internal condition is not met. The compiler notices this and complains with you

You should ensure that every possible path of execution returns something out of the called method to the caller.

For example

static string meth3(int i)     
{         
    string b = "buzz";          
    if (i == 5)         
    {             
        return b;         
    }      
    // What if the code reaches this point?
    // What do you return to the caller?
    return string.Empty; // or whatever you decide as correct default for this method
} 

Upvotes: 0

Matzi
Matzi

Reputation: 13925

Your functions only return when the if is evaluated to true. Add return statement outside the if also, or add else statement, and your code will compile and work.

static string meth2(int i)
{
    string f = "fuzz";

    if (0 == (i % 3))
    {
        return f;
    }
    else
      return "";
}

Upvotes: 0

Related Questions