Solved

An interesting observation re: VB6 & Dictionaries

Posted on 2006-07-10
5
256 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
5 Comments
 
LVL 142

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 20 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help in WHSCRIPT 9 47
Using "ScreenUpdating" 6 55
vb6 connector to mongodb 2 50
MS Access 03, TransferText, decimal places 8 45
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now