Dictionary Object Woes

Results 1 to 2 of 2

Thread: Dictionary Object Woes

  1. #1
    Matt Howe Guest

    Default Dictionary Object Woes

    I&#039m attempting to use the Dictionary object for a simple login. Here&#039s the code:<BR><BR>Dim LoginDic, UserName, PassWord<BR><BR>Set LoginDic = CreateObject("Scripting.Dictionary")<BR>LoginDic.A dd "user1", "password1"<BR>LoginDic.Add "user2", "password2"<BR>...<BR><BR>UserName = request.form("user")<BR>PassWord = request.form("pass")<BR><BR>If LoginDic.Exists(UserName) And LoginDic.Item(UserName) = PassWord Then<BR> &#039Login Succeeds<BR>Else<BR> &#039Login Fails<BR>End If<BR><BR>&#039It wasn&#039t working, so I tested it thus:<BR><BR>response.write LoginDic.Exists(UserName)<BR> &#039always displays "TRUE" unless I enter an explicit incorrect value, "wrongname" or something...<BR>response.write LoginDic.Item(UserName)<BR> &#039displays nothing unless I specify the username explicitly instead of using the variable.<BR><BR>What might I be doing wrong?<BR><BR>Thanks!<BR><BR>-Matt<BR><BR>

  2. #2
    Join Date
    Dec 1969

    Default RE: Dictionary Object Woes

    I cut and pasted your code and it worked just fine for me.<BR><BR>I suspect other errors.<BR><BR>I might note that you *DO* have a bug in that code, though it shouldn&#039t manifest itself in that simple test.<BR><BR>Specifically, when you do<BR>&nbsp;&nbsp;&nbsp;&nbsp;If LoginDic.Exists(UserName) And LoginDic.Item(UserName) = PassWord Then<BR>the second part, <BR>&nbsp;&nbsp;&nbsp;&nbsp;LoginDic.Item(UserName ) = PassWord<BR>will cause a key with that value to be added to the dictionary! It will be given a blank value. So the next time you use that same dictionary, you will find the username in place, but with no associated password.<BR><BR>See the docs for Dictionary.Item to see what I mean.<BR><BR>To fix the problem, do this:<BR><BR>If LoginDic.Exists(UserName) Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;If LoginDic.Item(UserName) = PassWord Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;...<BR>&nbsp;&nbsp;&nbsp;&nbsp;End If<BR>End If<BR><BR>(You would not need to do this in C++/Java/JavaScript, where the "&&" operator would not actually executed the second comparison if the first was false. But VB and VBS never "short circuit" any operations.)<BR><BR>ALMOST FORGOT: Since it sure appears to me to be your code, it looks like you better show more code.<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts