Morning, simple stupid question. I have found post with similar issues but after reading through it does not solve my error.
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;
}
}
}
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
or shorter