-7

this says need to return path but i cant reach if cases is there any way ? thanks

public IEnumerable<RequestCall> GetRequests(string erisim, string sube, string sicil)
{
    using (var redisclient = RedisManager.GetClient())
    {
        var redisUser = redisclient.As<RequestCall>();

        if (erisim == "A")
        {
            return redisUser.GetAll();// .Where(c=>c.Sube=="Y");
        }
        else if (erisim == "P")
        {
            return redisUser.GetAll().Where(c => c.Sube == sube);
        }
        else if (erisim == "C")
        {
            return redisUser.GetAll().Where(c => c.CagriAcan == sicil);
        }

    }
}

this says need to return path but i cant reach if cases is there any way ? thanks

svick
  • 236,525
  • 50
  • 385
  • 514
  • 2
    add one more `else` block and `return` the defualt value – Sudhakar Tillapudi Jun 30 '14 at 12:52
  • 1
    What if `erisim` isn't any of `A`, `P` or `C`? You're describing a function that's meant to return an `IEnumerable` but don't tell the compiler what to do in this case. – Damien_The_Unbeliever Jun 30 '14 at 12:54
  • its only char; example if erisim char == p return sube if erisim == c return sicil like – user3790322 Jun 30 '14 at 12:55
  • @user3790322 - that's not what the function signature says - you've defined it as `string` which means it *could* be `I am the very model of a modern major general` - there's nothing here to tell the compiler (or anyone else) that it's strictly limited to one of only 3 options. – Damien_The_Unbeliever Jun 30 '14 at 12:56
  • Can you clarify your question? You say "this says" - *what* says? I'd assume that you're getting a compilation error, but this isn't clear from your question. If you are getting a compilation error, can you include the *exact* text of the error? – Dan Puzey Jun 30 '14 at 13:08

4 Answers4

1

You could try this one:

if (erisim == "A")
{
    return redisUser.GetAll();// .Where(c=>c.Sube=="Y");
}
else if (erisim == "P")
{
    return redisUser.GetAll().Where(c => c.Sube == sube);
}
else if (erisim == "C")
{
    return redisUser.GetAll().Where(c => c.CagriAcan == sicil);
}
else
{
    return Enumerable.Empty<RequestCall>();
}
Christos
  • 53,228
  • 8
  • 76
  • 108
0

Sudhakar Tillapudi, absolutely right, you need to add deafult clause:

using (var redisclient = RedisManager.GetClient())
{
    var redisUser = redisclient.As<RequestCall>();

    if (erisim == "A")
    {
        return redisUser.GetAll();// .Where(c=>c.Sube=="Y");
    }
    else if (erisim == "P")
    {
        return redisUser.GetAll().Where(c => c.Sube == sube);
    }
    else if (erisim == "C")
    {
        return redisUser.GetAll().Where(c => c.CagriAcan == sicil);
    }
    else return null;
}
0

The problem is you aren't accounting for the scenario where erisim does not equal A, B or C therefore the compiler doesn't know what to return.

It's generally neater to have a single exit point in a function, so instead of having multiple return statements, have a single return value e.g.

var result = null;
if (erisim == "A") 
{
    result = ...;
} 
else if (erisim == "P") 
{
    result = ...;
} 
else if (erisim == "C") 
{
    result = ...;
}
return result;

This approach will force you to define something to return (even if none of the statements are true) so you should never get those types of compiler errors.

James
  • 80,725
  • 18
  • 167
  • 237
  • Um. The compiler error forces you to return something. You can't have a logical error and forget to return something. Unlike here where you can now have a logical error and forget to assign to `result` in one of your branches. Congratulations, you've transformed a potential compile time error into a potential runtime error. – Damien_The_Unbeliever Jun 30 '14 at 13:06
  • @Damien_The_Unbeliever well first of all, as far as I am aware the compiler won't let you use an unassigned variable so your logical error comment is void (little programming pun for ya). And secondly, that's exactly what I was referring to, the compiler is going to force you to return something, this approach forces you to *think* about what to return if none of those statements pass. It's purely a style choice and I was simply offering a different angle. – James Jun 30 '14 at 13:28
  • you assigned it `null` at the top of your `if` stack. The OP was *already* receiving a compiler error. – Damien_The_Unbeliever Jun 30 '14 at 13:29
  • @Damien_The_Unbeliever yeah, purely an example. `null` might be an acceptable return value for that function (I have no idea what the function does so I can't elaborate on that). The point was demonstrating setting up a default value and overriding it but having a single exit point. – James Jun 30 '14 at 13:31
  • But my point is now that someone comes along and adds a new `if` block - they do a bit of work and forget to assign `result`. This will not be caught by the compiler. Compare that with the original code/accepted answer where, if I had done the same but forgotten to `return` I'd now be getting a compiler error that not all paths return a result. – Damien_The_Unbeliever Jun 30 '14 at 13:32
  • @Damien_The_Unbeliever ah ok, I see the point you are making. However, that's easily resolved by not setting the initial default value at the top which would throw "*use of unassigned*" error if `result` was *never* set. Like I said, it's a style choice, the chances of the developer forgetting to set `result` using an approach like this is pretty slim in my opinion. However, I do take your point. – James Jun 30 '14 at 13:39
0

Just to clarify:

Not all code paths in your code are returning IEnumerable<RequestCall>. For example, what if I pass the parameter erisim the value "X". That won't pass the 1st, 2nd or 3rd if statements. Then, your method exits, without returning anything (which is giving you the error message).

param erisim:

   -> "A" -> return something
   -> "P" -> return something
   -> "C" -> return something
   -> Anything else -> (this part is missing!)

As others said, you need else to cover the remanining cases. Or just return Enumerable.Empty<RequestCall>(); before the end of the method.

Also, as a word of advice, you could use enums instead of "magic" strings if possible, or at least constants.

Conrad Clark
  • 4,533
  • 5
  • 45
  • 70