I have a problem.
The recursion is working fine as long as Item.Length > 0.
When Item.Length == 0 it skips the line: GetId(Id, result, Items);
Goes to the line return Id;
with the correct Id,
and then instead of leaving the function and return the Id,
it goes straight to the line GetId(Id, result, Items);
with the PREVIOUS Id, result and Items.
Where is the problem?
private string GetId(string Id, List<something> result, string[] Items)
{
try
{
// some code
if (Items.Length != 0)
GetId(Id, result, Items);
}
catch (Exception e){}
return Id;
}
Well yes, it would. Look at your code:
if (Items.Length != 0)
GetId(Id, result, Items);
Which part of that says that it should exit the method? It doesn't. It just calls GetId
and completely ignores the result.
Instead, you probably meant to return the value returned by the recursive call:
if (Items.Length != 0)
return GetId(Id, result, Items);
As an aside, I hope your real exception handling is rather better - catching Exception
is usually a bad idea, and silently swallowing it is pretty much always a bad idea...
See more on this question at Stackoverflow