Home > Mobile >  How to fix c# compiler error, "not all code paths return a value" Word.GetLetters
How to fix c# compiler error, "not all code paths return a value" Word.GetLetters

Time:01-30

Not all paths return a string value to return I have tried to make everything return a string value but still couldn't figure it out. I have tried lots of things, but can't fix the error. I tried result.null, but still got the error. Can someone help me, I can't figure out how to fix the error, I've been working for a while trying to fix the error, but can't figure it out.

public letters GetLetters()
{
   var result = new letters();
   var Words = new List<string> {"start"};
   if (Words.Count == 0)
   {
      return null;
   }
   foreach (var word in Words)
   {
      if (word == null)
      {
         return null;
       }
       else
       {
          if(word.Length == 5)
          {
              string YourWord = word;
              char[] letters = YourWord.ToCharArray();
              char a = letters[0];
              char b = letters[1];
              char c = letters[2];
              char d = letters[3];
              char e = letters[4];
              result.First = a.ToString();
              result.Second = b.ToString();
              result.Third = c.ToString();
              result.Fourth = d.ToString();
              result.Fifth = e.ToString();
              return result;
            }
            else
            {
               return null;
            }
     }
  }
}

CodePudding user response:

The easiest solution is to remove:

if (Words.Count == 0)
{
    return null;
}

and then after the loop put:

return null;

That way if Words is empty it will effectively skip the loop and then return null;. So no need to specifically check whether Count is 0. Plus you've made life easier for the compiler - it can see that every code path has a return.

That being said - I'd spend a little bit of time testing the code since your code ignores all except the first item in Words. Which is, well, weird.

It is also odd that your letters type is storing characters as strings.

CodePudding user response:

I'm not sure what exactly you are trying to do but, assuming you have Letters class as :

    public class Letters
    {
        public string? First { get; set; }
        public string? Second { get; set; }
        public string? Third { get; set; }
        public string? Fourth { get; set; }
        public string? Fifth { get; set; }
    }

then avoid deep nesting `if/else`` statement:

    public static Letters GetLetters()
    {
        var result = new Letters();
        var Words = new List<string> { "start" };
        if (Words.Count == 0)
        {
            return null;
        }

        foreach (var word in Words)
        {
            if (word == null)
            {
                return null;
            }
            if (word.Length == 5)
            {
                string YourWord = word;
                char[] letters = YourWord.ToCharArray();
                char a = letters[0];
                char b = letters[1];
                char c = letters[2];
                char d = letters[3];
                char e = letters[4];
                result.First = a.ToString();
                result.Second = b.ToString();
                result.Third = c.ToString();
                result.Fourth = d.ToString();
                result.Fifth = e.ToString();

                return result;
            }
        }

        return null;
    }
  • Related