Error 1: 'reversestring1.Class1.ProgrammingTest.ReverseString(string)': not all code paths return a value
y iam getting this error.please reply.thanks in advance.
Visual C# General
Error 1: 'reversestring1.Class1.ProgrammingTest.ReverseString(string)': not all code paths return a value
y iam getting this error.please reply.thanks in advance.
Hi,
This error is caused when all code paths don't have a return value. For eg: if you have an if-condition and code the return statement within it and don't have any return for the else-condition, you will get this error.
Make sure that you have a return statement for all your code paths.
Thanks,
Rashmi
public class ProgrammingTest
{
public string ReverseString(string word)
{
string inputString = "abcdefg";
string reversedString = string.Empty;
for (int c = inputString.Length; c > 0; c--)
{
reversedString += inputString[c - 1]
}
Console.WriteLine("Original String: " + inputString);
Console.WriteLine("Reversed String: " + reversedString);
}
}
hai Rashmi gopinath,this is my programme,now u tell me wr is the error.i am not using if condition.please reply.
thnaks.
You declared your method to return a string, which u arent doing.
This should do it:
Code Snippetpublic class ProgrammingTest
{
public string ReverseString(string word)
{
string inputString = "abcdefg";
string reversedString = string.Empty;
for (int c = inputString.Length; c > 0; c--)
{
reversedString += inputString[c - 1]
}
Console.WriteLine("Original String: " + inputString);
Console.WriteLine("Reversed String: " + reversedString);
return reversedString;
}
}
M. Dirksma is right. Your function needs to return the string. Just add "return reversedString;" as the last line of your code. Alternatively, if you dont want your function to return anything, you could declare it as "void".
Thanks,
Rashmi
Many thanks to M.Dirksma and Rashmi gopinath.