0

Alright, so I've been trying to fix this for a good while now. I have a "warning" within my code - "Unreachable Code Detected". I'm not sure how to fix this, which is why I'm posting here. Below is the code:

    public static byte Authenticate(string UserName, string Password)
    {
        try
        {
            MySqlDataAdapter DataAdapter = new MySqlDataAdapter("SELECT * FROM `Accounts` WHERE `AccountID` = '" + UserName + "'", Connection);
            DataSet DSet = new DataSet();

            DataAdapter.Fill(DSet, "Account");

            if (DSet != null && DSet.Tables["Account"].Rows.Count > 0)
            {
                DataRow DR = DSet.Tables["Account"].Rows[0];

                string Pass = (string)DR["Password"];
                if (Pass == Password || Pass == "")
                {
                    if (Pass == "")
                    {
                        MySqlCommand Command = new MySqlCommand("UPDATE `Accounts` SET `Password` = '" + Password + "' WHERE `AccountID` = '" + UserName + "'", Connection);
                        Command.ExecuteNonQuery();
                    }

                    uint LogonCount = (uint)DR["LogonCount"];
                    LogonCount++;

                    MySqlCommand Comm = new MySqlCommand("UPDATE `Accounts` SET `LogonCount` = " + LogonCount + " WHERE `AccountID` = '" + UserName + "'", Connection);
                    Comm.ExecuteNonQuery();

                    return Convert.ToByte((uint)DR["LogonType"]);
                }
                else
                    return 0;
            }
            else
                return 0;
        }

        catch (Exception Exc) { General.WriteLine(Exc.ToString()); return 0; General.ServerRestart(); }
    }

I don't know how to highlight it red, so I'll post the specific line:

    catch (Exception Exc) { General.WriteLine(Exc.ToString()); return 0; General.ServerRestart(); }

The warning is "General".ServerRestart();

  • 10
    When you hit `return 0;` at your highlighted line of code, do you naturally expect the statement that follows to execute? Because you shouldn't. Neither does the compiler. Ergo, it is marked as unreachable. – Anthony Pegram Mar 23 '12 at 03:05

5 Answers5

4

General.ServerRestart() must happen before you call return. Return immediately exits the method before any remaining code is not ran, hence the warning for "unreachable code"

mellodev
  • 1,597
  • 12
  • 20
2
catch (Exception Exc)
{
    General.WriteLine(Exc.ToString());
    return 0;
    General.ServerRestart();
}

In your catch block, you are returning a value and then performing extra functions.

When you write "return 0;", it doesn't "set" the return value to be 0 when the function completes, but it returns the value 0 as the result of the function and stops the function at that point. So when you have a return statement, any code afterwards will never be executed, because the function will terminate before that, at the return statement.

Note this scenario:

// ... some code 1

if (someExpression)
{
    // ... some code 2

    return;

    // ... some code 3
}

// ... some code 4

return;

// ... some code 5

The code1 will always execute. If someExpression is true - code2 will execute (and others won't), otherwise - code 4 will execute (and others won't). In any case, code3 and code5 won't execute, because in each of their perspective code paths, they come after a return statement.

SimpleVar
  • 14,044
  • 4
  • 38
  • 60
1

You are returning prematurely. Statements following a return will not be executed as they are "Unreachable".

torrential coding
  • 1,755
  • 2
  • 24
  • 34
1
    catch (Exception Exc) { General.WriteLine(Exc.ToString()); return 0; General.ServerRestart(); }

should be:

    catch (Exception Exc) { General.WriteLine(Exc.ToString()); General.ServerRestart(); return 0; }

You can't call General.ServerRestart after you've just exited the function.

McAden
  • 13,714
  • 5
  • 37
  • 63
0

when you return something, it ends the method. Its not possible to return multiple values, unless its a list. and even lists would be returned in one line like return ListName;

Oztaco
  • 3,399
  • 11
  • 45
  • 84
  • So should I switch them around like this?: catch (Exception Exc) { General.WriteLine(Exc.ToString()); General.ServerRestart(); return 0; } – Donavon Decker Mar 23 '12 at 03:09
  • "It's not possible to return multiple values"...? Someone hasn't heard about generators (pardon me...'enumerators'). :) – cHao Mar 23 '12 at 03:17