I want to check if a dictionary contains a key value pair.

sock1992

Well-known member
Joined
May 20, 2020
Messages
107
Programming Experience
Beginner
I had this working perfectly with if statements but I was duplicating the error message so I thought id try and simplify it with TryGetValue

The problem I have at the moment is, its still allowing me to sign in if the username is correct and the password is wrong.
 
Last edited:
First, you are just matching the username, but not the password.

Second, Look closely at your line 22. When the login attempts reaches the limit, then you exit the loop... Just as if you put in the correct password and exit the loop on line 14.
 
Dictionary.TryGetValue returns true if it contains an element with the specified key, and if so return the value from dictionary to the out parameter. You're missing checking the password.

Line 9 should be UserAccount useraccount = null; - it is assigned from dictionary if the key exists.
 
First, you are just matching the username, but not the password.

Second, Look closely at your line 22. When the login attempts reaches the limit, then you exit the loop... Just as if you put in the correct password and exit the loop on line 14.
Thanks for pointing that out on line 22, I don't know how I never noticed that ?
 
Back
Top Bottom