0

I declare the variable setPassword outside of the loop and then give it a value within the loop. Then in the next do-while loop I try to use the value assigned but it says "Use of unassigned local variable".
profile[I] is an array of objects that are created prior to the loops. Is the value being assigned in the loop not saving or is the value of profile[I].Password null because the object hasn't been created yet?

bool good = false;
string username;
do
{
    bool broke = false;

    Console.WriteLine("Please create a username");
    username = Console.ReadLine();
    for (int i = 0; i < profile.Count; i++)
    {
        if (username == profile[i].Username)
        {
            Console.WriteLine("The username already exists");
            broke = true;
            break;
        }

    }
    if (broke == false)
    {
        good = true;
    }


} while (good == false);
Console.WriteLine("Please create a password");
string password = Console.ReadLine();
profile.Add(new Users(username, password, 0));

string setPassword;
bool validUser = false;
do
{
    Console.Clear();
    Console.WriteLine("Enter your username");
    string tryUsername = Console.ReadLine();

    for (int i = 0; i < profile.Count; i++)
    {
        if (profile[i].Username == tryUsername)
        {
            setPassword = profile[i].Password;
            validUser = true;
        }
    }
    if (validUser == false)
    {
        Console.WriteLine("Invalid username. Usernames are case sensitive");
        Thread.Sleep(2500);
    }
} while (validUser == false);

bool validPass = false;
do
{
    Console.WriteLine("Enter your password");
    string tryPass = Console.ReadLine();
    if (tryPass == setPassword) //this is the error
    {
        validPass = true;
    }
    else
    {
        Console.WriteLine("Invalid password. Passwords are case sensitive");
    }
} while (validPass == false);

5 Answers 5

1

The compiler can't know it will actually get assigned (and it doesn't if not all if statements you have evaluate to true).

Assign a default value and you will be fine:

string setPassword = null;
Sign up to request clarification or add additional context in comments.

Comments

1

I initiate the variable setPassword outside of the loop and then give it a value within the loop.

This is the problem. The system cannot guarantee that a value is assigned before it is used.

It is possible that the loop iterates 0 times.
It is also possible that condition of the surrounding if statement evaluates to false.
Both of these situations lead to setPassword never getting a value.

So the compiler gives you an error, it is possible that you are using setPassword before it has a value.

The solution is to set it to a default value outside the loop, and outside the if.

4 Comments

I initiated the value to null but now I get invalid password even though I am entering the right password. validPass is never set to true for some reason even though I am entering the same exact characters that I put in for the password
@A.Barbee Have you checked what's in setPassword at the time that you compare them? It is generally advised to not use == to compare strings, but to use Equals instead. In this particular case, though, I don't think that is the problem.
@S.L.Barth Generally advised? Sais whom? I think it´s totally find to compare strings using ==. I know in Java there exists such a requirement, but not in C#. In particular if the string to be compared with is null you´ll get a NullReferenceException when using Equals.
@HimBromBeere Says Microsoft: msdn.microsoft.com/en-us/library/cc165449.aspx .
1

This is because the compiler can´t know that your for-loop is executed at least once and in particular that the if-statement within the loop also passes at least for one iteration of that loop. Thus - at least from the perspective of the compiler - it is possible that setPassword is never assigned a value and thus you get that error.

Assign null at the start:

string setPassword = null;

Comments

0

Basically the problem is this : You are using them in mostly the if statements, the if statements uses a variable. But you only declared but never defined the variable globally/locally, which automatically gives an error, despite the variable will be taking a user's input locally, the if statement is unfortunately too stupid to detect that for you, plus it also takes the possibility that the user skips the step of giving an input too. Hence, you need to set a default value. Like what they stated, you can use : string setPassword = null; or string setPassword = "";

[Don't need to mind nullables , strings can be null by default]

Comments

0

To solve your problem, you should assign setPassword to string.Empty, null, or some other value, based on your use case

If you are curious about - Why does the compiler complain that the variable is unassigned even though you assigned a value to it in while loop?

This is called the Definite Assignment behavior of the C# language. A variable is considered to be definitely assigned if

  1. The variable was initialized at the time of declare - either with a default value or an explicitly value
  2. Otherwise, if the compiler can prove, by static flow analysis (in simple words, compile time checks), that all possible execution paths leading up to the first use of variable will assign a value to the variable. Note, the static flow analysis is the key here, the compiler does not evaluate or take for granted that any run-time possibilities (conditions in if, while, for etc. control statements) will eventually assign the variable a value.

See Definite assignment at MSDN for more info. It is an archived document but should still be good a reference. Also DotNetPerls Page describes it in simple language.

Disclaimer: I have no association with DotNetPerls.

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.