Solved

Explanation needed for VB code

Posted on 2011-03-24
7
363 Views
Last Modified: 2013-11-27
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.Text, users(i), True) = 0)
5)    Dim validPassword As Boolean = (String.Compare(Password.Text, 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.
0
Comment
Question by:adamtrask
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35206123
The True/False switch that gets passed to the String.Compare method controls case-sensitivity for the comparison. So, in essence it is doing a case-insensitive comparison of username, but case-sensitive for passwords.

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.
0
 

Author Comment

by:adamtrask
ID: 35206226
Carl,

Are you saying that if I wrote Line 4 like this:

4)     Dim validUsername As Boolean = (String.Compare(UserName.Text, 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?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35206243
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
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

by:adamtrask
ID: 35206267
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.Text, users(i)) = 0)
5)    Dim validPassword As Boolean = (String.Compare(Password.Text, passwords(i)) = 0)
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 35206332
You can, but that will always use a case-sensitive comparison.

I think your code is being explicit because usual practice for login credentials is for usernames to be case-insensitive, while passwords are usually case-sensitive.
0
 

Author Comment

by:adamtrask
ID: 35206426
Thanks you so much.
This cleared a mystery for me.
0
 

Author Closing Comment

by:adamtrask
ID: 35206430
Great response
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question