C
C#•7mo ago
olleeee

✅ Why is a variable false in project in WPF .Net application even after it should be true?

Hi I'm currently building my first C# project and could really need some help, I'm supposed to take in a password, check if it is correct to the declared passwords in properties and then be able to change the password if the new password follows the right format. Most of the code i have done but struggling a bit now, so could really really need some help.
63 Replies
SinFluxx
SinFluxx•7mo ago
$details
MODiX
MODiX•7mo ago
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, and what you expect the result to be. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
olleeee
olleeee•7mo ago
Oh okey! sorry im new here and also find it hard to explain the error but will upload the code!
SinFluxx
SinFluxx•7mo ago
That's ok 🙂just people need to be able to see what you're doing to be able to help. And if you're sending code follow the guidance here $code
MODiX
MODiX•7mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
olleeee
olleeee•7mo ago
oh okey thank you! I have put the whole code now in the mod, but how does people find it?
olleeee
olleeee•7mo ago
BlazeBin - tnkliwnraizr
A tool for sharing your source code with the world!
olleeee
olleeee•7mo ago
soo whats going on in my code is that im typing in a password, that password is suppose to open the door for different dormitories in Hogwarts, griffendor, hufflepuff, slytherin or ravenclaw. I have succeeded in the part to open the door when puting in the right password, but when checking if the new password is in the right format something happens. Then it says that the checkedpassword is false, which means i cant go further in the code.
SinFluxx
SinFluxx•7mo ago
So you mean the problem is in your BtnCheckpassword_Click method?
olleeee
olleeee•7mo ago
no, i think the method CheckPassWord is wrong, i put in a break point and see that even though i put return passwordChecked == true; its still false, which i dont understand
SinFluxx
SinFluxx•7mo ago
Where is your CheckPassword method?
olleeee
olleeee•7mo ago
BlazeBin - tnkliwnraizr
A tool for sharing your source code with the world!
olleeee
olleeee•7mo ago
sorry didnt realise when i put the class and the cs file in different windows it would change the link okej im really sorry but sort of worked that error out now....
SinFluxx
SinFluxx•7mo ago
No sorry I hadn't spotted there was an extra tab, why are you using a for loop in there but comparing the whole string anyway? Not sure why you've got house specific fields in your Houses class either
olleeee
olleeee•7mo ago
so i should take away the for loop?
SinFluxx
SinFluxx•7mo ago
Well it's not doing anything right now Because you're just checking the full password entered against the Password property of that house
olleeee
olleeee•7mo ago
ohh okej thank you.
// public bool CheckPassWord(string passwordEntered)
{

if (passwordEntered == Password)
{
MessageBox.Show($"You said the right password, welcome to {HomeName}");
return passwordChecked == true;
}
else if (passwordEntered == slytherinPassword)
{
MessageBox.Show($"You said the right password, welcome to {HomeName}");
return passwordCheckedSlytherin == true;
}
if (passwordEntered != Password)
{
MessageBox.Show("You said the wrong password");
return passwordChecked == false;
}
return false;
}
// public bool CheckPassWord(string passwordEntered)
{

if (passwordEntered == Password)
{
MessageBox.Show($"You said the right password, welcome to {HomeName}");
return passwordChecked == true;
}
else if (passwordEntered == slytherinPassword)
{
MessageBox.Show($"You said the right password, welcome to {HomeName}");
return passwordCheckedSlytherin == true;
}
if (passwordEntered != Password)
{
MessageBox.Show("You said the wrong password");
return passwordChecked == false;
}
return false;
}
so i have updated the method to this instead, but when running the code the message you said wrong password comes up 3 times but then still allows me to go to the next part of the program
SinFluxx
SinFluxx•7mo ago
That'll be because you're checking the password against all 4 houses, really your CheckPassword method should just return true or false and not do anything with messages
olleeee
olleeee•7mo ago
okey so you saying take away the messagebox and put that somewhere else?
SinFluxx
SinFluxx•7mo ago
Yeah, then your CheckPassword only has one job, and you can then see what all 4 results are before deciding which message to show
olleeee
olleeee•7mo ago
so where can i put the messagebox instead? in the main window? but then HomeName doesnt work
SinFluxx
SinFluxx•7mo ago
You could still do e.g. hufflepuff.HouseName
olleeee
olleeee•7mo ago
but then i have to see which password have been put in, because before the program could see that the password matched the homeName and put it there? i put the messagebox here now instead, but when doing that all the messages comes up and says that i have said the right password
public void BtnCheckpassword_Click(object sender, RoutedEventArgs e)
{
string passwordEntered = Txtenteredpassword.Text;



bool checkedpasswordgriffendor = griffendor.CheckPassWord(passwordEntered);
bool checkedpasswordhufflepuff = hufflepuff.CheckPassWord(passwordEntered);
bool checkedpasswordravenclaw = ravenclaw.CheckPassWord(passwordEntered);
bool checkedpasswordslytherin = slytherin.CheckPassWord(passwordEntered);

if (checkedpasswordgriffendor == true)
{
MessageBox.Show($"You said the right password for {griffendor.HomeName}");

BtnNewPassword.Visibility = Visibility.Visible;
TxtnewPassword.Visibility = Visibility.Visible;
labelhidden.Visibility = Visibility.Visible;

}
if (checkedpasswordravenclaw == true)
{
MessageBox.Show($"You said the right password for {ravenclaw.HomeName}");

BtnNewPassword.Visibility = Visibility.Visible;
TxtnewPassword.Visibility = Visibility.Visible;
labelhidden.Visibility = Visibility.Visible;
}

if (checkedpasswordhufflepuff == true)
{
MessageBox.Show($"You said the right password for {hufflepuff.HomeName}");

BtnNewPassword.Visibility = Visibility.Visible;
TxtnewPassword.Visibility = Visibility.Visible;
labelhidden.Visibility = Visibility.Visible;

}
if (checkedpasswordslytherin == true)
{
MessageBox.Show($"You said the right password for {slytherin.HomeName}");

BtnNewPassword.Visibility = Visibility.Visible;
TxtnewPassword.Visibility = Visibility.Visible;
labelhidden.Visibility = Visibility.Visible;
}
public void BtnCheckpassword_Click(object sender, RoutedEventArgs e)
{
string passwordEntered = Txtenteredpassword.Text;



bool checkedpasswordgriffendor = griffendor.CheckPassWord(passwordEntered);
bool checkedpasswordhufflepuff = hufflepuff.CheckPassWord(passwordEntered);
bool checkedpasswordravenclaw = ravenclaw.CheckPassWord(passwordEntered);
bool checkedpasswordslytherin = slytherin.CheckPassWord(passwordEntered);

if (checkedpasswordgriffendor == true)
{
MessageBox.Show($"You said the right password for {griffendor.HomeName}");

BtnNewPassword.Visibility = Visibility.Visible;
TxtnewPassword.Visibility = Visibility.Visible;
labelhidden.Visibility = Visibility.Visible;

}
if (checkedpasswordravenclaw == true)
{
MessageBox.Show($"You said the right password for {ravenclaw.HomeName}");

BtnNewPassword.Visibility = Visibility.Visible;
TxtnewPassword.Visibility = Visibility.Visible;
labelhidden.Visibility = Visibility.Visible;
}

if (checkedpasswordhufflepuff == true)
{
MessageBox.Show($"You said the right password for {hufflepuff.HomeName}");

BtnNewPassword.Visibility = Visibility.Visible;
TxtnewPassword.Visibility = Visibility.Visible;
labelhidden.Visibility = Visibility.Visible;

}
if (checkedpasswordslytherin == true)
{
MessageBox.Show($"You said the right password for {slytherin.HomeName}");

BtnNewPassword.Visibility = Visibility.Visible;
TxtnewPassword.Visibility = Visibility.Visible;
labelhidden.Visibility = Visibility.Visible;
}
SinFluxx
SinFluxx•7mo ago
What does your updated CheckPassword method look like?
olleeee
olleeee•7mo ago
public bool CheckPassWord(string passwordEntered)
{
if (passwordEntered == Password)
{
return passwordChecked == true;
}
else if (passwordEntered == slytherinPassword)
{
return passwordCheckedSlytherin == true;
}
else if (passwordEntered != Password)
{
return passwordChecked == false;
}
return false;
}
public bool CheckPassWord(string passwordEntered)
{
if (passwordEntered == Password)
{
return passwordChecked == true;
}
else if (passwordEntered == slytherinPassword)
{
return passwordCheckedSlytherin == true;
}
else if (passwordEntered != Password)
{
return passwordChecked == false;
}
return false;
}
SinFluxx
SinFluxx•7mo ago
Why is it checking a slytherinPassword on all the houses?
olleeee
olleeee•7mo ago
so the assigment say that slytherin house have different criterias when choosing a new password, therefor i wanted to be sure if it was a slytherin password or not and use the passwordchecked slytherin further down in the code
SinFluxx
SinFluxx•7mo ago
But your other houses shouldn't know what Slytherin's password is
olleeee
olleeee•7mo ago
no so now i took the slytherin part away, but something weird then happens!
public bool CheckPassWord(string passwordEntered)
{
if (passwordEntered == Password)
{
return passwordChecked == true;
}
else if (passwordEntered != Password)
{
return passwordChecked == false;
}
return false;
}
public bool CheckPassWord(string passwordEntered)
{
if (passwordEntered == Password)
{
return passwordChecked == true;
}
else if (passwordEntered != Password)
{
return passwordChecked == false;
}
return false;
}
the ravenclaw password opens the griffendor door
SinFluxx
SinFluxx•7mo ago
Ok so, this won't fix the issue but may make it easier to read, you BtnCheckpassword_Click method can be shortened:
public void BtnCheckpassword_Click(object sender, RoutedEventArgs e)
{
string passwordEntered = Txtenteredpassword.Text;
bool passwordMatches = false;
string houseName = "";

if (griffendor.CheckPassWord(passwordEntered))
{
passwordMatches = true;
houseName = griffendor.HouseName;
}

if (hufflepuff.CheckPassWord(passwordEntered))
{
passwordMatches = true;
houseName = hufflepuff.HouseName;
}

if (ravenclaw.CheckPassWord(passwordEntered))
{
passwordMatches = true;
houseName = ravenclaw.HouseName;
}

if (slytherin.CheckPassWord(passwordEntered))
{
passwordMatches = true;
houseName = slytherin.HouseName;
}


if (passwordMatches)
{
MessageBox.Show($"You said the right password for {houseName}");
BtnNewPassword.Visibility = Visibility.Visible;
TxtnewPassword.Visibility = Visibility.Visible;
labelhidden.Visibility = Visibility.Visible;
}
}
public void BtnCheckpassword_Click(object sender, RoutedEventArgs e)
{
string passwordEntered = Txtenteredpassword.Text;
bool passwordMatches = false;
string houseName = "";

if (griffendor.CheckPassWord(passwordEntered))
{
passwordMatches = true;
houseName = griffendor.HouseName;
}

if (hufflepuff.CheckPassWord(passwordEntered))
{
passwordMatches = true;
houseName = hufflepuff.HouseName;
}

if (ravenclaw.CheckPassWord(passwordEntered))
{
passwordMatches = true;
houseName = ravenclaw.HouseName;
}

if (slytherin.CheckPassWord(passwordEntered))
{
passwordMatches = true;
houseName = slytherin.HouseName;
}


if (passwordMatches)
{
MessageBox.Show($"You said the right password for {houseName}");
BtnNewPassword.Visibility = Visibility.Visible;
TxtnewPassword.Visibility = Visibility.Visible;
labelhidden.Visibility = Visibility.Visible;
}
}
Where's the code where the door gets opened?
olleeee
olleeee•7mo ago
well i guess it is the checkpassword part now all of the password says that its a slytherin password thats been entered.
public bool CheckPassWord(string passwordEntered)
{
if (passwordEntered == Password)
{
return passwordChecked == true;
}
else if (passwordEntered != Password)
{
return passwordChecked == false;
}
return false;

}
public bool CheckPassWord(string passwordEntered)
{
if (passwordEntered == Password)
{
return passwordChecked == true;
}
else if (passwordEntered != Password)
{
return passwordChecked == false;
}
return false;

}
SinFluxx
SinFluxx•7mo ago
Can you upload all your code again please?
olleeee
olleeee•7mo ago
of course!
olleeee
olleeee•7mo ago
BlazeBin - zdejdsuaktar
A tool for sharing your source code with the world!
olleeee
olleeee•7mo ago
BlazeBin - zdejdsuaktar
A tool for sharing your source code with the world!
olleeee
olleeee•7mo ago
okej i got that sorted actually, just added the == true after griffendor.CheckPassWord(passwordEntered)
public void BtnCheckpassword_Click(object sender, RoutedEventArgs e)
{
string passwordEntered = Txtenteredpassword.Text;
bool passwordMatches = false;
string houseName = "";

if (griffendor.CheckPassWord(passwordEntered) == true)
{
passwordMatches = true;
houseName = griffendor.HomeName;
}
public void BtnCheckpassword_Click(object sender, RoutedEventArgs e)
{
string passwordEntered = Txtenteredpassword.Text;
bool passwordMatches = false;
string houseName = "";

if (griffendor.CheckPassWord(passwordEntered) == true)
{
passwordMatches = true;
houseName = griffendor.HomeName;
}
but now the changing of password doesnt work, could you please help me with that? really appreciate the help i have already got!
SinFluxx
SinFluxx•7mo ago
that shouldn't have changed anything if (somethingThatReturnsBool()) is the same as: if (somethingThatReturnsBool() == true)
olleeee
olleeee•7mo ago
i didnt change anything else so dont know what happended then
olleeee
olleeee•7mo ago
BlazeBin - avcoftwpimrz
A tool for sharing your source code with the world!
SinFluxx
SinFluxx•7mo ago
yeah I'm a bit confused because if I run it my end, without your latest change, it shows me the message for the correct house
olleeee
olleeee•7mo ago
hmmm okej thats very weird okey that works for me as well now
SinFluxx
SinFluxx•7mo ago
What problems were you having with changing the password?
olleeee
olleeee•7mo ago
its the last part i think, and it is when im changing the slythering password. So i created a method only for the change of the slytherin password, and trying now to change it.
public bool RightFormatPasswordSlytherin(string passwords)
{
char[] konsonanter = { 'b', 'c', 'd', 's', 'z', 'x', 'q', 'w', 'r', 'f', 't', 'g', 'v', 'h', 'j', 'n', 'm', 'k', 'l', 'p' };
if (passwords.Length > 8)
{
char firstchar = char.ToLower(passwords[0]);
char lastchar = char.ToLower(passwords[passwords.Length - 1]);

foreach (char konsonant in konsonanter)
{
if (firstchar == konsonant && lastchar == konsonant)
{
return true;

}
}
}
return false;
}
public bool RightFormatPasswordSlytherin(string passwords)
{
char[] konsonanter = { 'b', 'c', 'd', 's', 'z', 'x', 'q', 'w', 'r', 'f', 't', 'g', 'v', 'h', 'j', 'n', 'm', 'k', 'l', 'p' };
if (passwords.Length > 8)
{
char firstchar = char.ToLower(passwords[0]);
char lastchar = char.ToLower(passwords[passwords.Length - 1]);

foreach (char konsonant in konsonanter)
{
if (firstchar == konsonant && lastchar == konsonant)
{
return true;

}
}
}
return false;
}
but when running the code it says the firstchar is not == konsonant and instead of the new password, the old password comes up.
SinFluxx
SinFluxx•7mo ago
So for the Slytherin password the first and last character have to be the same?
olleeee
olleeee•7mo ago
it have to be a consonant one of the consonants in the array
SinFluxx
SinFluxx•7mo ago
if (firstchar == konsonant && lastchar == konsonant) This check you're doing means that it will only return true if the first and last letters are the same consonant
olleeee
olleeee•7mo ago
ohh okey, so no wonder should i do a for loop instead and go through all the consonants?
SinFluxx
SinFluxx•7mo ago
Are you allowed to use Linq?
olleeee
olleeee•7mo ago
no its suppose to be fully your own methods
SinFluxx
SinFluxx•7mo ago
thought that would be the case 🙂
olleeee
olleeee•7mo ago
i dont really understand because the first method works,
public bool RightFormatPassword(string passwords)
{
char[] vokaler = { 'a', 'e', 'i', 'o', 'u', 'y', 'å', 'ä', 'ö' };
char[] konsonanter = { 'b', 'c', 'd', 's', 'z', 'x', 'q', 'w', 'r', 'f', 't', 'g', 'v', 'h', 'j', 'n', 'm', 'k', 'l', 'p' };
if (passwords.Length > 5)
{
char firstchar = char.ToLower(passwords[0]);
char lastchar = char.ToLower(passwords[passwords.Length - 1]);
foreach (char vokal in vokaler)
{
if (firstchar == vokal)
{

foreach (char konsonant in konsonanter)
{
if (lastchar == konsonant)
{
return true;
}
}
}
}
} return false;

}
public bool RightFormatPassword(string passwords)
{
char[] vokaler = { 'a', 'e', 'i', 'o', 'u', 'y', 'å', 'ä', 'ö' };
char[] konsonanter = { 'b', 'c', 'd', 's', 'z', 'x', 'q', 'w', 'r', 'f', 't', 'g', 'v', 'h', 'j', 'n', 'm', 'k', 'l', 'p' };
if (passwords.Length > 5)
{
char firstchar = char.ToLower(passwords[0]);
char lastchar = char.ToLower(passwords[passwords.Length - 1]);
foreach (char vokal in vokaler)
{
if (firstchar == vokal)
{

foreach (char konsonant in konsonanter)
{
if (lastchar == konsonant)
{
return true;
}
}
}
}
} return false;

}
yeah unfortunately
SinFluxx
SinFluxx•7mo ago
that's because there you're checking the two characters separately you can still do one loop and do something like:
bool firstCharIsKonsonant = false;
bool lastCharIsKonsonant = false;

foreach (char konsonant in konsonanter)
{
if (firstchar == konsonant)
firstCharIsKonsonant = true;

if (lastChar == konsonant)
lastCharIsConsonant = true;
}

return firstCharIsKonsonant && lastCharIsKonsonant; // this will return true only if both are true
bool firstCharIsKonsonant = false;
bool lastCharIsKonsonant = false;

foreach (char konsonant in konsonanter)
{
if (firstchar == konsonant)
firstCharIsKonsonant = true;

if (lastChar == konsonant)
lastCharIsConsonant = true;
}

return firstCharIsKonsonant && lastCharIsKonsonant; // this will return true only if both are true
olleeee
olleeee•7mo ago
so when i do that i get the error that operator == cant be used on char and char{} **char[]
SinFluxx
SinFluxx•7mo ago
Show your code?
olleeee
olleeee•7mo ago
public bool RightFormatPasswordSlytherin(string passwords)
{
bool firstCharisKonsonant = false;
bool lastcharisKonsonant = false;
char[] konsonanter = { 'b', 'c', 'd', 's', 'z', 'x', 'q', 'w', 'r', 'f', 't', 'g', 'v', 'h', 'j', 'n', 'm', 'k', 'l', 'p' };
if (passwords.Length > 8)
{
char firstchar = char.ToLower(passwords[0]);
char lastchar = char.ToLower(passwords[passwords.Length - 1]);

foreach(char konsonant in konsonanter)
{
if (firstchar == konsonanter)
{ firstCharisKonsonant == true; }


if (lastchar == konsonanter)
{ lastcharisKonsonant == true; }

}
} return lastcharisKonsonant && firstCharisKonsonant;

}
public bool RightFormatPasswordSlytherin(string passwords)
{
bool firstCharisKonsonant = false;
bool lastcharisKonsonant = false;
char[] konsonanter = { 'b', 'c', 'd', 's', 'z', 'x', 'q', 'w', 'r', 'f', 't', 'g', 'v', 'h', 'j', 'n', 'm', 'k', 'l', 'p' };
if (passwords.Length > 8)
{
char firstchar = char.ToLower(passwords[0]);
char lastchar = char.ToLower(passwords[passwords.Length - 1]);

foreach(char konsonant in konsonanter)
{
if (firstchar == konsonanter)
{ firstCharisKonsonant == true; }


if (lastchar == konsonanter)
{ lastcharisKonsonant == true; }

}
} return lastcharisKonsonant && firstCharisKonsonant;

}
SinFluxx
SinFluxx•7mo ago
if (firstchar == konsonanter) if (lastchar == konsonanter) you mistyped konsonanter instead of konsonant
if (firstchar == konsonanter)
{ firstCharisKonsonant == true; }
if (firstchar == konsonanter)
{ firstCharisKonsonant == true; }
When you have something like an if statement and it only has one line inside, you don't need to have {} (but it doesn't matter either way)
olleeee
olleeee•7mo ago
ohhh nooo 😆 silly little erros
SinFluxx
SinFluxx•7mo ago
also inside your if statements you've put ==, but it should just be = 🙂 everyone makes them!
olleeee
olleeee•7mo ago
ahhh i cant thank you enough for all of the help!!
SinFluxx
SinFluxx•7mo ago
I mean that for here: firstCharisKonsonant == true; lastcharisKonsonant == true; to be clear!
olleeee
olleeee•7mo ago
yes! changed and now the whole program workS!!!
Ikarmus
Ikarmus•7mo ago
consider $close
MODiX
MODiX•7mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server
More Posts