Solved

An interesting observation re: VB6 & Dictionaries

Posted on 2006-07-10
5
259 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
using Access 8 83
Getting warning: You are about to delete 1 row(s) 9 53
Spell Check in VB6 13 137
Passing a Text Box name to a Sub 6 102
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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

749 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