Dictionary and set (dictionary and Collection)

Source: Internet
Author: User

The dictionary object replaces the collection object and provides additional languages to increase and delete records by three times faster than before.

Although Visual Basic 6.0 has only a few new features, it has some powerful new object models, one of which is a dictionary object.

Dictionary objects are new versions of ubiquitous visual basic collection objects. Its introduction exists in VBScript 2.0 and uses Visual Basic 6.0 to support scripting Runtime Library to cover all content of Visual Basic. At the beginning, the dictionary object was only contained in VBScript and responded to the web group request as the equivalent body of Perl-related content.

Similar to collection objects, you can store any type of data or dictionary objects through dictionary. These data and objects are generally considered as part of the dictionary, and each part is assigned a string key value. Although I don't think Microsoft intends to free you from the headache of collecting and replacing the above data and objects, in fact, in the previous Visual Basic 6.0 documents, dcitionary objects are seldom mentioned, therefore, I think this is an important feature of Visual Basic 6.0.


Comparison between a dictionary object and a collection object

Starting from Visual Basic 4.0, collection objects replace user-defined types as the main data types. Therefore, most visual basic programs will contain collection objects. If you have been very accustomed to using collection objects since Visual Basic 4.0, why do you need to make changes? There are several main factors:
A dictionary object is faster than a collection object. This speed advantage mainly lies in the addition of data members, iterative search and deletion of data members in the dictionary.
Dictionary objects include encapsulated functions that you often have to compile yourself, such as exists functions and removeall functions.
The dictionary object allows you to create an array of key and item values to accelerate iterative search in the dictionary.
The dictionary object allows you to overwrite existing key values and existing data members. Dictionary objects do have the following disadvantages, but they are not worth mentioning:
Unlike collection objects, a dictionary object is not part of a vba dll, which means you need to use scrrun. dll and connect it to the corresponding application.

Dictionary object implementation... Each... The next loop method is also very strange. It does not return the item value, but the key value.
Another annoying feature of a dictionary object is that if you want to delete an unsearched member from the dictionary, you must add data to this empty entry or a key that does not exist.
Access a dictionary object, as I mentioned earlier, a dictionary object is not a specific part of the VBA or visual basic real-time language. It is an object that exists in Microsoft scripting Runtime Library (scrrun. dll. To use a dictionary object in an application, you must use the reference dialog box to addProject-level reference to scripting Runtime Library.

After adding a reference, create a dictionary object instance as follows:
Dim odict as Dictionary
Set odict = new dictionary
'Do some work.
Set odict = nothing

To add a member to a dictionary object, the add method includes two parameters: the data to be added and the string key value associated with the data. The syntax is as follows:


Dictionary. Add key, Data

The dictionary object does not specify the new data member storage location parameter. It will be picked out by the dictionary itself. You also need to note that the parameters of the add method are the opposite of the add method of the collection object. In the collection object:


Collection. add data, [Key], [before], [after]

Similar to collection objects, a dictionary object can be a member of any data type, object, or other dictionary, so that you can nest a dictionary object as you wish.

Access a member of a dictionary object

The item method of a dictionary object is a recommended method for accessing data contained in the dictionary. Its advantage is that it is fast and fast. My tests show that the access speed to data members of a dictionary object is three times faster than that of data members of a collection object. If you open the Object Browser, select a dictionary object, and observe hidden members, you will see the attribute hashval, which indicates that the dictionary object has a list of useless information and some strange queuing algorithms.

When designing a dictionary object, the string-type key value is used as a parameter to pass to the item method for data access. This is similar to the Collection object, for example, you can use:


Vitem = odictionary. Item (skey)

Here, we warn that an error (Code 5, invalid procedure call or argument) will occur if you try to return the value of a collection member using a key value that does not exist ).

This is not the case for a dictionary object. when inserting a new member, it uses a non-existing key value to correspond to a key and uses a zero-length string to correspond to data members. A dictionary object always checks whether the key you want to use exists in the dictionary. As you can imagine, this feature can easily capture errors that you accidentally make, the method for checking the existence of key values will be described later in this article.

When using collection objects, you cannot directly access the data in the dictionary in sequence, but this is not the case when using the dictionary item method. You can quickly create an array of all data members, and access all data in sequence using this array:
Dim vitems as Variant
Dim iordinal as integer

Iordinal = 10
Vitems = odictionay. Items
Vitem = vitems (iordinal)

The method for deleting data from collection objects is usually... Each... Next statement, when you first use... Each... Next, you can assume that you have never used this statement for the dictionary, but you can still use... Each... Next, you only need the entry-related key value returned by the inter_newenum function of the dictionary object, instead of returning the index of the dictionary entry like the collection object, you can pass these key values to the item method to delete data members, as shown below:
Dim skey as Variant

For each skey in odictionary
Vitem = odictionary. Item (skey)
...
Next

When you use a dictionary object in the encapsulation class, another application that uses... Each... The secondary key of next. You cannot use... Each... Next loop performs iterative search on data members, unless you are willing to perform a lot of complex programming. The reason is that the internal_newenum function of the dictionary object is not an implicit member, but in the collection object, it cannot be called Through Visual Basic, therefore, you cannot implement your own _ newenum function in the encapsulation class, simply set newenum = mcol. the [_ newenum] statement cannot work with a dictionary object. However, the many benefits of using a dcitionary object make this compromise very worthwhile.

So how to access every member of the dictionary object encapsulation class? A dictionary object contains a method named items. This method returns a variable array of all dictionary object members. You only need to provide an encapsulation subroutine in your class to return the item array:
Public property get items () as Variant
Items = mddict. Items
End Property

Or if you want to provide a more meaningful name for the encapsulation feature, you can:
Public property get employees () as Variant
Employees = mddict. Items
End Property

Then your client code can use... Each... Next or... The next loop performs iterative search in a variable array. The following code shows you how to achieve this:
Dim oemployees as employees 'wrapper class
Dim aemployees as variant 'variant to hold Array
Dim oemp as employee 'data member class
Dim I as integer 'simple counter

Set oemployees = new employees 'dictionary Wrapper class
Aemployees = oemployees. Employees 'Return an array of Objects

For I = lbound (aemployees) to ubound (aemployees)
Set oemp = aemployees (I)
Cbonames. additem oemp. Name
Set oemp = nothing
Next I
Set oemployees = nothing

So what is the performance? When a dynamic Connection Library is called on the same machine, the item array of the dictionary encapsulation class and foe... Each... Next's iterative search is not as fast as iterative search by using only collection encapsulation classes, but if you are dealing with remote or out-of-process service programs, the opposite is true. Using the dictionary encapsulation class, you only perform simple conversion of simple arrays, while the collection class calls the remote service program repeatedly, and each iteration calls the process.
I set up a simple experiment to check the migration of remote dictionary objects and collection objects. These objects include 1000 simple string members and use them to migrate a list of client forms, it takes only 1/4 seconds for a dictionary object to migrate the list, and it takes about three seconds for the Collection object to migrate the list.
Does your member exist?
One of the reasons I repeatedly complain about a collection object is its inability to let you know in advance whether a member of the collection object exists. If the Member's key value does not exist, then you have to handle the error. For this reason, I usually use a class to encapsulate the collection object members and include the exists attributes.

In any case, Microsoft makes the dictionary object have the exists method. Exists is very easy to use and returns true or false, as shown below:
If odictionary. exsits (skey) then
'The key is there.
Vval = odictionary. Item (skey)
Else
Msgbox "the key doesn't exist"
End if

Because a dictionary object always adds a key value and an empty string to a member, when you try to return an entry that does not have a key value, you can always use the exists method to detect the existence of an entry before returning it (as shown in the preceding example). This feature prevents you from directly accessing a key value that does not exist.

Key Value Overwrite
If you have tried to change the key value corresponding to a collection object member, you know this is impossible. When an object member is added to a collection object, the data and key values of the member are fixed. The only choice you can make is to use the Remove Method to clear the Member and add a new member to the object. However, you can use the key feature of the dictionary object to overwrite the key value of the key, as shown in the following example:
If odictionary. exists (soldkey) then
'The key is there.
Odictionary. Key (soldkey) = snewkey
Else
Msgbox "the key dosen't exsit"
End if

Member coverage
I guess when Microsoft Compiled collection objects, they assumed that the collection object members would not change once they joined. Why do they think that developers only deal with static data ?! Therefore, the only way to change the collection object members is to delete them from the collection object and add them again.

Similar to the key feature, you can use the item feature of a dictionary object on both sides of the expression. On the right side of an expression, you return the value of the object member. On the left side of the expression, you can set the value of the member as follows:
If odictionary. exists (skey) then
'The key is there.
Odictionary. Item (skey) = vnewitem
Else
Msgbox "the key doesn't exist"
End if

Supplement
When you need an array of all key values in the dictionary, the item method and key method can also help you. The item method can return a variable array containing all the data members in the dictionary, while the key method can return a variable array containing all the key values in the dictionary. Other features of a dictionary object include the Count feature of returning the number of members in the dictionary and the comparemode feature that allows you to control the execution of Internal searches, as well as the remove feature and removeall feature, as shown in its name, they are used to clear data members in the dictionary.

Summary
A dictionary object is a very valuable attempt compared to a collection object. It is not only fast, but also has many features that free you from the troubles of having to compile your own encapsulation classes. Although it takes some secondary record technology to replace collection objects with a dictionary object (according to... Each... But the performance improvement brought about by the dictionary object is enough to compensate for these efforts. The professional resource CD in this topic includes
This section describes how to create a class named dictclass for a dictionary object. the CLs encapsulation class also includes an example application, which shows you how to use these classes to obtain more powerful functions than your application.

Collection is quite popular. Most of the Visual Basic data classes originate from this class, And the dictionary object is an important improvement. It is three times faster than the collection object in adding and deleting object members, you can dramatically improve application performance. You can also test and compare the performance of the dictionary object and collection object on your own, and you will get roughly the same result as mine.

If I have the freedom to choose (my customers have enough time and money), I will replace all collection objects with a dictionary object. This work is not started yet, at least not going on this week, but from now on, all programs I have compiled using Visual Basic will use a dictionary object.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.