views:

193

answers:

7

Why does this code not compile?
It gives me the error:

not all code paths return a value

Code:

public bool isUserProfileHashed(string username)
{
    bool isHashed = false;
    MembershipUser u = null;
    u = Membership.GetUser(username);
    if (u != null)
    {
        try
        {
            u.GetPassword();                   
        }
        catch (Exception exception)
        {
            // An exception is thrown when the GetPassword method is called for a user with a hashed password
            isHashed = true;
            return isHashed;
        }
        return isHashed;
    }
+10  A: 

You forgot to put a return outside of the if, put it after the if ending brace

public bool isUserProfileHashed(string username)
{
    bool isHashed = false;
    MembershipUser u = null;
    u = Membership.GetUser(username);
    if (u != null)
    {
        try
        {
            u.GetPassword();                   
        }
        catch
        {
            // An exception is thrown when the GetPassword method is called for a user with a hashed password
            isHashed = true;
        }
    }
    return isHashed;
}

[Edit]
Remove unnecessary return (@Fredrik Mörk)
Caught exception not used hence removed it as well.

Binoj Antony
You are probably aware of it already, but you could actually remove the return statment in the catch-block as well; it doesn't add anything.
Fredrik Mörk
yes, did not pay attention, will update the answer
Binoj Antony
+1  A: 

There is no return statement outside of the if block. Sometimes that if block may not execute.

Malfist
+1  A: 

No return if u is null.

Joel Goodwin
+1  A: 

If u == null, your entire if-statement will be skipped over. There is no return statement outside of your if statement.

That is why you see, "not all code paths return a value."

Just add a return statement after your if block:

public bool isUserProfileHashed(string username)
{
    bool isHashed = false;
    MembershipUser u = null;
    u = Membership.GetUser(username);
    if (u != null)
    {
        // ...
        return isHashed;
    }

    // more code here if you need it

    return ??? ; // <--- **ADD THIS**
}
Robert Cartaino
+1  A: 

You're not handling the case where u == null and returning a value if that condition is met.

Jamie Ide
+1  A: 

If u happens to be null you don't return!

This should work better

public bool isUserProfileHashed(string username)
{
    bool isHashed = false;
    MembershipUser u = null;
    u = Membership.GetUser(username);
    if (u != null)
    {
        try
        {
            u.GetPassword();                   
        }
        catch (Exception exception)
        {
            // An exception is thrown when the GetPassword method is called for a user with a hashed password
            isHashed = true;
            return isHashed;
        }
        return isHashed;
    }
    else
    { 
        //Throw or return false, whatever
        throw new Exception("Could not get user ...");
    }
}
C. Ross
+1  A: 

The correction to your method has been noted by other posters, however, would it be possible to replace the entire method with this line?

if(Membership.Provider.PasswordFormat == MembershipPasswordFormat.Hashed)
Traples