1

I want to get any non empty value from my string array (starting from reverse). I want to use recursive function for achieving my goal. Below is sample of code I have written.

protected string getInvNo(string[] arg, int lengthV)
{
    if (arg[lengthV] != "")
        return arg[lengthV];
    else if (lengthV >= 0)
        getInvNo(arg, lengthV-1);
    else
        return "";
}

It's showing error not all path return value. Requested to help me what's I am missing. Thanks.

5
  • 2
    change getInvNo(arg, lengthV-1); to return getInvNo(arg, lengthV-1); Commented Apr 18, 2017 at 6:18
  • int cnt = 0; protected string getInvNo(string[] arg, int lengthV) { cnt = ++cnt; if (cnt > 10) { return ""; } if (arg[lengthV] != "") { return arg[lengthV]; } else if (lengthV >= 0) { return getInvNo(arg, lengthV - 1); } else { return ""; } } Commented Apr 18, 2017 at 6:20
  • Please avoid adding the solution to the question's body. Instead, either delete your post or answer your own question and accept if so that other people will know that the problem is solved. Commented Apr 18, 2017 at 6:24
  • Sure. Thanks for help. Commented Apr 18, 2017 at 6:26
  • Please don't add your solution to the question. If it's radically different to any of the existing answers post it as an answer of your own. Commented Apr 18, 2017 at 7:55

3 Answers 3

1

I suggest a bit redesign your code:

  1. You've omitted return in the getInvNo(arg, lengthV-1);
  2. What if array's item is null? If we have to skip it as well as empty "" one, string.IsNullOrEmpty is the choice
  3. Add validation (since method is protected one I can inherit your class and call the method with any arguments I like)
  4. Change method into static

Implementation:

// static: you don't use "this" in the context, and the method is not a virtual one
protected static string getInvNo(string[] arg, int index)
{
    // Validation
    if (null == arg)
      throw new ArgumentNullException("arg");
    else if (index >= arg.Length)
      index = arg.Length - 1; // or throw ArgumentOutOfRangeException("index"); 

    // Stop conditions:
    if (index < 0)                               // 1. Array exhausted
        return ""; 
    else if (!string.IsNullOrEmpty(arg[index]))  // 2. We've found non-empty value
        return arg[index];

    return getInvNo(arg, index - 1);
}

Please, notice that usually we solve such problems without recoursion:

using System.Linq;

...

string result = arg.LastOrDefault(item => !string.IsNullOrEmpty(item)) ?? "";
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thanks for explanation.
1

lengthV > 0 will fix the index going out of range to -1.

static string getInvNo(string[] arg, int lengthV)
{
    if (arg[lengthV] != "")
    {
        return arg[lengthV];
    }

    return lengthV > 0 ? getInvNo(arg, lengthV - 1) : "";
}

Comments

-1

Return here:

if (arg[lengthV] != "")
       return arg[lengthV];
else if (lengthV >= 0)
      getInvNo(arg, lengthV-1);
      return "this is your missing return value";
else
      return "";

7 Comments

Hi, I have doubt about working of this code. Is it length will be decrease constantly till 0? or it will get stuck till length - 1 ?
I am answering your problem "It's showing error not all path return value. Requested to help me what's I am missing"
I don't know what is the result of this function else if (lengthV >= 0) getInvNo(arg, lengthV-1); the important is you have to return a string value on that part.
Solved the problem. Thanks for help.
I didn't downvote your answer but it is wrong. It will not return the value from the array if the last item of the array is an empty string, but return the hard coded string.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.