Help error CS0165

Hi, in regards to the interfaces project, I’ve been getting an error from a type of code I’ve done several times now and I’m not sure where is the issue.
As seen in bellow,I use the for loop to create an asterisk for each character in Password in the Display() method however I keep getting the following error:
PasswordManager.cs(28,11): error CS0165: Use of unassigned local variable ‘a’ [/home/ccuser/workspace/learn-csharp-interfaces-inheritance-app-interfaces/SavingInterface.csproj]

The build failed. Fix the build errors and run again.

This is the code I used:
public void Display()

{

if(Hidden==false)

{

Console.WriteLine(Password);

}

else

{

for(int a;a==Password.Length;a++)

{

Console.Write("*");

}

}

}

Hello @py3597878804 , welcome to the forums! Notice you don’t actually assign a anything?

Notice the a==? == is the equality operator; it checks for equality. = is the assignment operator…

Hi Codeneutrino,
So I changed it to for(int a;a=Password.Length;a++) and I still get the following error:
PasswordManager.cs(28,11): error CS0029: Cannot implicitly convert type ‘int’ to ‘bool’ [/home/ccuser/workspace/learn-csharp-interfaces-inheritance-app-interfaces/SavingInterface.csproj]

The build failed. Fix the build errors and run again.

What I don’t understand is that password.Length should be an int, password itself was string and was never bool, the only thing that was bool is hidden.
How do I convert password’s length so I can use it in the for loop?

using System;

namespace SavingInterface
{
class PasswordManager:IDisplayable
{
private string Password
{ get; set; }

public bool Hidden
{ get; private set; }

public PasswordManager(string password, bool hidden)
{
  Password = password;
  Hidden = hidden;
}

public void Display()
{
if(Hidden==false)
{
Console.WriteLine(Password);

}
else
{

for(int x;x=password.Length;x++)
{
Console.Write("*");

}

}
}
}
}

Thank you.

Okay, I realised I made a dumb mistake and forgot to add int=0.
I corrected that and got no errors, however when I use the Display method neither the asterisks nor the password appear.
in the main program I call:
using System;

namespace SavingInterface
{
class Program
{
static void Main(string args)
{
TodoList tdl = new TodoList();
tdl.Add(“Invite friends”);
tdl.Add(“Buy decorations”);
tdl.Add(“Party”);
tdl.Display();
PasswordManager pm = new PasswordManager(“iluvpie”, true);
pm.Display();
}
}
}

However what I get is
$ dotnet run
Invite friends
Buy decorations
Party

No password or asterisks to be seen for pm.Display().
for reference the class where the Display method is the following:

using System;

namespace SavingInterface

{

class PasswordManager:IDisplayable

{

private string Password

{ get; set; }

public bool Hidden

{ get; private set; }

public PasswordManager(string password, bool hidden)

{

  Password = password;

  Hidden = hidden;

}

public void Display()

{

if(Hidden==true)

{

for(int x=0;x==Password.Length;x++)

{

Console.Write("*");

}

}

else

{

Console.WriteLine(Password);

}

}

}

}

Hello! Could you please format your code according to this post? It’s quite difficult to debug currently.

The condition part of your for is wrong. The for loop will only keep going while the condition is true. Think of the for statement like so:

// First part of the for initialise variables
int x = 0;

// Condition part of the statement, do the loop while this is true.
while(x == Password.Length) 
{
    // Do your work
    Console.Write("*");
    
    // Do iterator section of for statement
    x++;
}

Obviously x == Password.Length isn’t true at the start, so the work you want done will never run.