help - "subscript out of range" - when iterating through a dictionary object

Author Message
elunderdog18

  • Total Posts : 12
  • Scores: 0
  • Reward points : 0
  • Joined: 10/19/2009
  • Status: offline
help - "subscript out of range" - when iterating through a dictionary object Friday, November 18, 2011 9:04 AM (permalink)
0
[Helpful answer received] / [List Solutions Only]
Hi buddies!
I pieced together a script that gets the name/date of birth of a any person and stores it in the corresponding dictionary name/value pairs
just before the script finishes executing, it iterates again and this time it should also display the name/value pairs for the newly added entries, but however, just before it starts message boxing out the new entries, I get a run-time error that blows the script off, saying
"subscript out of range: '4'"

here is the entire script posted for those of you willing to help me out in this one. thanks in advice guys. happy early thanksgiving
Option Explicit
'-----------------------------------------------------------------------------
Dim objDictionary, keys, i, prompt, NewKey, NewItem 'explicit declaration of variables
Set objDictionary = WScript.CreateObject("Scripting.Dictionary") 'instantianting the dictionary object for the objName object variable

'addings keys/items to the dictionary
objDictionary.Add "marvin", "May 25, 1966"
objDictionary.Add "melany", "October 22, 1962"
objDictionary.Add "bryan", "May 4, 1990"
objDictionary.Add "iliana", "October 2, 1993"

keys = objDictionary.keys 'assinging the keys collection to a variable
For i = 0 To objDictionary.Count -1 'minus one because keys collection index starts at 0, prevents a out-of-range Error
 MsgBox "family member: " & keys(i) & " DOB: " & objDictionary.Item(keys(i))'grabs the corresponding item for the key indicated by the i counter variable
Next

prompt = MsgBox("do you want to add another person and their DOB?", vbYesNo + vbQuestion +  vbDefaultButton2, "Add More keys/values")

If prompt = vbNo Then
 MsgBox "GOOD-BYE", vbOKOnly + vbExclamation, "Quiting script"
 WScript.quit
Else
 Do 
 NewKey = LCase(InputBox("please enter a name","New Name", "Maria"))
 If NewKey = "" Then
 Exit Do 
 ElseIf Not objDictionary.Exists(NewKey) Then
 NewItem =InputBox("please enter a DOB for : " & NewKey & VbCrLf & "Use this format: Month Day, Year, ""January 6, 2000""", "DOB", "April 11, 1997")
 objDictionary.Add NewKey, NewItem
 Else
 MsgBox "The name you entered already exists, enter a different name", vbOKOnly + vbCritical, "Invalid Key"
 End If
 Loop While NewKey <> "" Or NewItem <> ""

End If

MsgBox "total number of entries in the dictionary: " & objDictionary.Count
 For i =  0 To objDictionary.Count 
 MsgBox "family member: " & keys(i) & " DOB: " & objDictionary.Item(keys(i))
 Next
 

<message edited by elunderdog18 on Friday, November 18, 2011 9:05 AM>
 
#1
    59cobalt

    • Total Posts : 981
    • Scores: 91
    • Reward points : 0
    • Joined: 7/17/2011
    • Status: offline
    Re:help - "subscript out of range" - when iterating through a dictionary object Friday, November 18, 2011 11:34 AM (permalink)
    2
    [This post was marked as helpful]
    elunderdog18
    just before it starts message boxing out the new entries, I get a run-time error that blows the script off, saying
    "subscript out of range: '4'"
    That's because your variable "keys" holds a copy of the keys in the dictionary at the point where you made the assignment. It does not hold a reference to the .Keys property of the dictionary. Compare the output of UBound(keys) and objDictionary.Count before each loop.

    Your whole iteration is unnecessarily complicated BTW (error-prone too, as you discovered yourself). Change it to this:
    For Each member In objDictionary.Keys
     MsgBox "family member: " & member & " DOB: " & objDictionary(member)
    Next
    so you won't run into index-related problems in the first place.
     
    #2
      elunderdog18

      • Total Posts : 12
      • Scores: 0
      • Reward points : 0
      • Joined: 10/19/2009
      • Status: offline
      Re:help - "subscript out of range" - when iterating through a dictionary object Saturday, November 19, 2011 4:11 PM (permalink)
      0
      thanks 59cobalt, now that I looked over my code with your explanation of the problem, it makes much more sense and now I am able to run the script with no subscript out-of -range problems
       
      have a happy thanksgiving
       
      #3

        Online Bookmarks Sharing: Share/Bookmark

        Jump to:

        Current active users

        There are 0 members and 1 guests.

        Icon Legend and Permission

        • New Messages
        • No New Messages
        • Hot Topic w/ New Messages
        • Hot Topic w/o New Messages
        • Locked w/ New Messages
        • Locked w/o New Messages
        • Read Message
        • Post New Thread
        • Reply to message
        • Post New Poll
        • Submit Vote
        • Post reward post
        • Delete my own posts
        • Delete my own threads
        • Rate post

        2000-2012 ASPPlayground.NET Forum Version 3.9