adamtrask
asked on
Explanation needed for VB code
Hello,
I need some clarification for the following lines of code which check the entered User name and entered password and if valid allow the user to login. The lines of code are numbered so that I can refer to the parts that I don't understand:
1) Dim users() As String = {"Adam", "Eve", "Angel"}
2) Dim passwords() As String = {"pass1", "pass2", "pass3"}
3) For i As Integer = 0 To users.Length - 1
4) Dim validUsername As Boolean = (String.Compare(UserName.T ext, users(i), True) = 0)
5) Dim validPassword As Boolean = (String.Compare(Password.T ext, passwords(i), False) = 0)
6) If validUsername AndAlso validPassword Then
7) ' TODO: Log in the user...
The above code works great. But what I don't understand are lines 4 and 5. I do understand what both lines are doing: they are comparing the text in the UserName with that of User(i) and if the outcome of the comparison is true then the user name is verified. The same logic is used in line 5 for the password.
But two things confuse me. The first is the way each line is constructed and second the fact that in line 4 "True" is used while in line 5 "False" is used. Shouldn't both of them be "True"?
Actually when I altered line 5 (from False to True) I got the same result! Why?
I need the clearest explanation and not the quickest. Thank you.
I need some clarification for the following lines of code which check the entered User name and entered password and if valid allow the user to login. The lines of code are numbered so that I can refer to the parts that I don't understand:
1) Dim users() As String = {"Adam", "Eve", "Angel"}
2) Dim passwords() As String = {"pass1", "pass2", "pass3"}
3) For i As Integer = 0 To users.Length - 1
4) Dim validUsername As Boolean = (String.Compare(UserName.T
5) Dim validPassword As Boolean = (String.Compare(Password.T
6) If validUsername AndAlso validPassword Then
7) ' TODO: Log in the user...
The above code works great. But what I don't understand are lines 4 and 5. I do understand what both lines are doing: they are comparing the text in the UserName with that of User(i) and if the outcome of the comparison is true then the user name is verified. The same logic is used in line 5 for the password.
But two things confuse me. The first is the way each line is constructed and second the fact that in line 4 "True" is used while in line 5 "False" is used. Shouldn't both of them be "True"?
Actually when I altered line 5 (from False to True) I got the same result! Why?
I need the clearest explanation and not the quickest. Thank you.
ASKER
Carl,
Are you saying that if I wrote Line 4 like this:
4) Dim validUsername As Boolean = (String.Compare(UserName.T ext, users(i),False) = 0)
It would make a case-sensitive comparison?
And if I used True instead of False in line 5 it would make a case-insensitive comparison?
Are you saying that if I wrote Line 4 like this:
4) Dim validUsername As Boolean = (String.Compare(UserName.T
It would make a case-sensitive comparison?
And if I used True instead of False in line 5 it would make a case-insensitive comparison?
True means ignore case, i.e. case-insensitive. False means make a case-sensitive comparison.
Reference:
http://msdn.microsoft.com/en-us/library/zkcaxw5y.aspx
Reference:
http://msdn.microsoft.com/en-us/library/zkcaxw5y.aspx
ASKER
One last clarification:
In lines 4 and 5, can I skip True and False altogether? and write them in the following way:
4) Dim validUsername As Boolean = (String.Compare(UserName.T ext, users(i)) = 0)
5) Dim validPassword As Boolean = (String.Compare(Password.T ext, passwords(i)) = 0)
In lines 4 and 5, can I skip True and False altogether? and write them in the following way:
4) Dim validUsername As Boolean = (String.Compare(UserName.T
5) Dim validPassword As Boolean = (String.Compare(Password.T
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks you so much.
This cleared a mystery for me.
This cleared a mystery for me.
ASKER
Great response
String.Compare will return 0 if the two strings match. You cannot assign 0 directly to a boolean, so those lines wrap the String.Compare(...) = 0 in brackets to create an expression that will return a boolean value.