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.
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?
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:
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.