Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

An interesting observation re: VB6 & Dictionaries

Posted on 2006-07-10
5
Medium Priority
?
263 Views
Last Modified: 2013-12-25
I use dictionaries extensively to keep track of data within the program.

In this case, I was using the dictionary to determine which records should be deleted.

Unfortunetley, all records were being deleted (test only, thank goodness!).

This is what I learned:

If you are stupid, like I was apparently, and get the sequence mixed up just a wee little  bit...

vKey = gRecord.Field("MasterKey")
sDictData = gDict.Item(vKey)
If Not gDict.Exists(vKey) Then SkipStuff
... process deletes

----------------

That code would ADD the value to the data dictionary, regardless of whether it is there or not!

vKey = gRecord.Field("MasterKey")
If Not gDict.Exists(vKey) Then SkipStuff
   sDictData = gDict.Item(vKey)
   ... process deletes

---------------

This works fine.

Thought I would share this... and perhaps,  if someone has an observation to share back, that would be equally interesting.
0
Comment
Question by:merydion
[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
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17078870
This is a typical Programming Logic Error, which is quite difficult to debug if code is not commented.
please comment the code so you and others will understand it later also.
0
 

Author Comment

by:merydion
ID: 17084286
The code is commented, I just didn't do so here for brevity.
0
 
LVL 18

Accepted Solution

by:
JR2003 earned 60 total points
ID: 17111262
See also:

http://www.microsoft.com/technet/scriptcenter/guide/sas_scr_ybdz.mspx?mfr=true

Inadvertently Adding a Key to a Dictionary

One potential problem in using the Dictionary object is that any attempt to reference an element that is not contained in the Dictionary does not result in an error. Instead, the nonexistent element is added to the Dictionary. Consider the following script sample, which creates a Dictionary, adds three key-item pairs to the Dictionary, and then attempts to echo the value of a nonexistent item, Printer 4:

When the script tries to echo the value of the nonexistent item, no run-time error occurs. Instead, the new key, Printer 4, is added to the Dictionary, along with the item value Null. As a result, the message box shown in Figure 4.9 appears.

Figure 4.9 Message Resulting from Inadvertently Adding a Key to a Dictionary

To avoid this problem, check for the existence of a key before trying to access the value of the item.

0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

636 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