Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6220265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:55:41+00:00 2026-05-24T07:55:41+00:00

I am looking up keys in a Scripting.Dictionary to make sure I add them

  • 0

I am looking up keys in a Scripting.Dictionary to make sure I add them (and their items) only once to the dictionary:

If MyDict.Exists (Key) Then ' first internal key-value lookup
  Set Entry=MyDict.Item (Key)  ' Second internal key->value lookup
else
  Set Entry=New ItemType
  MyDict.Add Key,Entry
End If
' Now I work on Entry...

Exists looks up the key in the dictionary, and Item () does it, too. So i get two key lookups for one logical lookup operation. Isn’t there a better way?

The dox for the Item property say

“If key is not found when attempting to return an existing item, a new
key is created and its corresponding item is left empty.”
(MSDN)

This is true, i.e. looking up a non-existant key obviously makes this key part of the dictionary, probably with associated item = empty.
But what is that good for? How could I use this to boil it down to one lookup operation? How can I set the empty item after creating the key during the Item () property call?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:55:42+00:00Added an answer on May 24, 2026 at 7:55 am

    The key problem to me is the fact that VBScript forces us to use Set with objects, and Empty is not an object. One trick that I’ve used in the past to get around this is to use the Array function to create a temporary placeholder for the value. Then I can check the array to see if the value is an object or not. Applied to your example:

    tempArr = Array(dict.Item(key))
    If IsEmpty(tempArr(0)) Then
        ' new entry
        Set entry = New MyClass
        ' can't use Add because the key has already been implicitly created
        Set dict.Item(key) = entry
    Else
        ' existing entry
        Set entry = tempArr(0)
    End If
    

    In this case, though, you haven’t eliminated the double lookup, but moved it from the “existing entry” case to the “new entry” case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for way to change all array's keys to one key for all
I'm looking for a dictionary type .NET collection supported a repeating keys and values,
I'm looking for a way to map some hot-keys to delete the line that
What is the shortcut in eclipse to terminate debugging/running? Looking under Preferences -> Keys
Just started looking into encryption using keys and certificates in sql server 2005/08 and
In Java I'm looking for a way to map multiple keys to the same
I'm looking for most fastest/effective way of deleting certain keys in a python dict
I am looking out for implementing a timestamp based cache with multiple keys. What
I am looking for advice on where to store encryption keys and other sensitive
I am looking to create a dictionary with 'roll-back' capabilities in python. The dictionary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.