1

i am writing C# function now, which must return me user password. All is fine, but than i am trying to compile this project, VS telling me about unassigned variable.

string sqlLoginCheck (string userId)
    {
        string database, host, user, pass, sqlParams, sqlQuery;
        string resultPassword;

        database = "db";
        host = "localhost";
        user = "root";
        pass = "....";

        sqlParams = "Database=" + database + ";Data Source=" + host + ";User Id=" + user + ";Password=" + pass;
        sqlQuery = "SELECT `id`, `name`, `pass` FROM `users` WHERE name = '" + userId + "' LIMIT 1";

        MySqlConnection sqlConnection = new MySqlConnection(sqlParams);
        MySqlCommand sqlCommand = new MySqlCommand(sqlQuery, sqlConnection);

        // Выполняем
        try
        {
            sqlConnection.Open();

            MySqlDataReader sqlReader = sqlCommand.ExecuteReader();

            if (sqlReader.HasRows)
            {
                while (sqlReader.Read())
                {
                    resultPassword = sqlReader[2].ToString();
                }
            }
            else
            {
                resultPassword = "";
            }
        }

        catch (MySqlException sqlError)
        {
            errorMessage.Text = "Ошибка: " + sqlError.Message;

            FileInfo errorLog = new FileInfo("errorLog.txt");

            if (errorLog.Exists == false)
            {
                FileStream errorFs = errorLog.Create();
                errorFs.Close();
            }

            StreamWriter errorSw = errorLog.AppendText();

            errorSw.WriteLine(DateTime.Now + " | " + sqlError.Message);
            errorSw.Close();
        }

        finally
        {
            sqlConnection.Close();
        }

        return resultPassword;

    }   

Here i can't return resultPassword, because variable is unassigned :|

0

5 Answers 5

2

Declare this variable as

string resultPassword = null;

Or you can declare it as

string resultPassword = String.Empty;

in that case you don't really need

else
{
            resultPassword = "";
}

because default value for password has already been set at declaration.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you and all who answered, this helped!
1

The resultPassword is declared in the function body, but it is initialized only inside the try block: so in case of exception, the variable will be unassigned.

Initialize the variable outside the try-catch block.

Some details here.

Comments

0

The compiler sees that there is a chance that resultPassword never gets assigned (if you go into the Catch or Finally. Just do :

string resultPassword = ""; 

in the declaration

Comments

0

It's because there is a root through the method where resultPassword is never assigned a value. As others have said declare:

string resultPassword = null;

Comments

0

I would suggest

string resultPassword = ""; 

then if you need to set to not found, actually set it to say "** not found **" or something. As it stands you have paths down your code where the variable isnt set - eg it enters the catch area.

Comments

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.