C
C#•12mo 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•12mo ago
$details
MODiX
MODiX•12mo 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
olleeeeOP•12mo ago
Oh okey! sorry im new here and also find it hard to explain the error but will upload the code!
SinFluxx
SinFluxx•12mo 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•12mo 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
olleeeeOP•12mo ago
oh okey thank you! I have put the whole code now in the mod, but how does people find it?
olleeee
olleeeeOP•12mo ago
BlazeBin - tnkliwnraizr
A tool for sharing your source code with the world!
olleeee
olleeeeOP•12mo 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•12mo ago
So you mean the problem is in your BtnCheckpassword_Click method?
olleeee
olleeeeOP•12mo 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•12mo ago
Where is your CheckPassword method?
olleeee
olleeeeOP•12mo ago
BlazeBin - tnkliwnraizr
A tool for sharing your source code with the world!
olleeee
olleeeeOP•12mo 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•12mo 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
olleeeeOP•12mo ago
so i should take away the for loop?
SinFluxx
SinFluxx•12mo 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
olleeeeOP•12mo 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•12mo 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
olleeeeOP•12mo ago
okey so you saying take away the messagebox and put that somewhere else?
SinFluxx
SinFluxx•12mo 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
olleeeeOP•12mo ago
so where can i put the messagebox instead? in the main window? but then HomeName doesnt work
SinFluxx
SinFluxx•12mo ago
You could still do e.g. hufflepuff.HouseName
olleeee
olleeeeOP•12mo 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•12mo ago
What does your updated CheckPassword method look like?
olleeee
olleeeeOP•12mo 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•12mo ago
Why is it checking a slytherinPassword on all the houses?
olleeee
olleeeeOP•12mo 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•12mo ago
But your other houses shouldn't know what Slytherin's password is
olleeee
olleeeeOP•12mo 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•12mo 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
olleeeeOP•12mo 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•12mo ago
Can you upload all your code again please?
olleeee
olleeeeOP•12mo ago
of course!
olleeee
olleeeeOP•12mo ago
BlazeBin - zdejdsuaktar
A tool for sharing your source code with the world!
olleeee
olleeeeOP•12mo ago
BlazeBin - zdejdsuaktar
A tool for sharing your source code with the world!
olleeee
olleeeeOP•12mo 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•12mo ago
that shouldn't have changed anything if (somethingThatReturnsBool()) is the same as: if (somethingThatReturnsBool() == true)
olleeee
olleeeeOP•12mo ago
i didnt change anything else so dont know what happended then
olleeee
olleeeeOP•12mo ago
BlazeBin - avcoftwpimrz
A tool for sharing your source code with the world!
SinFluxx
SinFluxx•12mo 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
olleeeeOP•12mo ago
hmmm okej thats very weird okey that works for me as well now
SinFluxx
SinFluxx•12mo ago
What problems were you having with changing the password?
olleeee
olleeeeOP•12mo 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•12mo ago
So for the Slytherin password the first and last character have to be the same?
olleeee
olleeeeOP•12mo ago
it have to be a consonant one of the consonants in the array
SinFluxx
SinFluxx•12mo 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
olleeeeOP•12mo ago
ohh okey, so no wonder should i do a for loop instead and go through all the consonants?
SinFluxx
SinFluxx•12mo ago
Are you allowed to use Linq?
olleeee
olleeeeOP•12mo ago
no its suppose to be fully your own methods
SinFluxx
SinFluxx•12mo ago
thought that would be the case 🙂
olleeee
olleeeeOP•12mo 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•12mo 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
olleeeeOP•12mo ago
so when i do that i get the error that operator == cant be used on char and char{} **char[]
SinFluxx
SinFluxx•12mo ago
Show your code?
olleeee
olleeeeOP•12mo 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•12mo 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
olleeeeOP•12mo ago
ohhh nooo 😆 silly little erros
SinFluxx
SinFluxx•12mo ago
also inside your if statements you've put ==, but it should just be = 🙂 everyone makes them!
olleeee
olleeeeOP•12mo ago
ahhh i cant thank you enough for all of the help!!
SinFluxx
SinFluxx•12mo ago
I mean that for here: firstCharisKonsonant == true; lastcharisKonsonant == true; to be clear!
olleeee
olleeeeOP•12mo ago
yes! changed and now the whole program workS!!!
Ikarmus
Ikarmus•12mo ago
consider $close
MODiX
MODiX•12mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server