✅ 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
$details
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)
Oh okey! sorry im new here and also find it hard to explain the error but will upload the code!
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
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/oh okey thank you! I have put the whole code now in the mod, but how does people find it?
BlazeBin - tnkliwnraizr
A tool for sharing your source code with the world!
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.
So you mean the problem is in your BtnCheckpassword_Click method?
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
Where is your CheckPassword method?
BlazeBin - tnkliwnraizr
A tool for sharing your source code with the world!
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....
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
so i should take away the for loop?
Well it's not doing anything right now
Because you're just checking the full password entered against the Password property of that house
ohh okej thank you.
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
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
okey so you saying take away the messagebox and put that somewhere else?
Yeah, then your CheckPassword only has one job, and you can then see what all 4 results are before deciding which message to show
so where can i put the messagebox instead? in the main window? but then HomeName doesnt work
You could still do e.g. hufflepuff.HouseName
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
What does your updated
CheckPassword
method look like?Why is it checking a
slytherinPassword
on all the houses?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
But your other houses shouldn't know what Slytherin's password is
no
so now i took the slytherin part away, but something weird then happens!
the ravenclaw password opens the griffendor door
Ok so, this won't fix the issue but may make it easier to read, you
BtnCheckpassword_Click
method can be shortened:
Where's the code where the door gets opened?well i guess it is the checkpassword part
now all of the password says that its a slytherin password thats been entered.
Can you upload all your code again please?
of course!
BlazeBin - zdejdsuaktar
A tool for sharing your source code with the world!
BlazeBin - zdejdsuaktar
A tool for sharing your source code with the world!
okej i got that sorted actually, just added the == true after griffendor.CheckPassWord(passwordEntered)
but now the changing of password doesnt work, could you please help me with that? really appreciate the help i have already got!
that shouldn't have changed anything
if (somethingThatReturnsBool())
is the same as:
if (somethingThatReturnsBool() == true)
i didnt change anything else so dont know what happended then
BlazeBin - avcoftwpimrz
A tool for sharing your source code with the world!
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
hmmm okej thats very weird
okey that works for me as well now
What problems were you having with changing the password?
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. but when running the code it says the firstchar is not == konsonant
and instead of the new password, the old password comes up.
So for the Slytherin password the first and last character have to be the same?
it have to be a consonant
one of the consonants in the array
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 consonantohh okey, so no wonder
should i do a for loop instead and go through all the consonants?
Are you allowed to use Linq?
no
its suppose to be fully your own methods
thought that would be the case 🙂
i dont really understand because the first method works,
yeah unfortunately
that's because there you're checking the two characters separately
you can still do one loop and do something like:
so when i do that i get the error that operator == cant be used on char and char{}
**char[]
Show your code?
if (firstchar == konsonanter)
if (lastchar == konsonanter)
you mistyped konsonanter
instead of konsonant
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)ohhh nooo 😆 silly little erros
also inside your if statements you've put
==
, but it should just be =
🙂
everyone makes them!ahhh i cant thank you enough for all of the help!!
I mean that for here:
firstCharisKonsonant == true;
lastcharisKonsonant == true;
to be clear!yes! changed and now the whole program workS!!!
consider $close
Use the /close command to mark a forum thread as answered