Dictionary object in VB

Source: Internet
Author: User

Dictionary object in VB
Core summary:
1. Replace the key name with obj. Key ("XX") = "newxx"
2. Change the key value or access the specified key:
(1) resetting the key value: obj. Item ("XX") = "newxx ",
(2) When the key "XX" in OBJ. Item ("XX") = "newxx" is not set, a key-value pair is added to the end of the object.
3. Obtain the number of entries: obj. Count (starting from 1)
4. Add key-value pairs: obj. Add key, Value
5. Remove key: obj. Remove ("key"), obj. removeall ()
6. Determine whether the key exists: obj. exists ("key ")
7. traversal: First assign the keys and values to a variable to form a one-dimensional array, and then use for traversal.
A = D. Keys 'B = D. Items
For I = 0 to D. Count-1
A (I)
B (I)
Next
8. Use OBJ .. comparemode = 0 (binary, default, case sensitive) OR = 1 (text, case insensitive, But not later) to specify the comparison mode,

'-----------------------------------
(1) dim D as new dictionnary
(2) dim D as dictionnary
Set d = new dictionnary
(3) dim D as dictionnary
Set d = Createobject ("scripting. Dictionary ")
'-------------------
Dim A, D, I 'creates some variables
Set d = Createobject ("scripting. Dictionary ")
D. Add "A", "Athens" 'to add some keywords and entries.
D. Add "B", "Belgrade"

Blnisthere = D. exists ("key1 ")
Stritem = D. Item ("key2 ")
Stritem = D. Remove ("key3 ")
D. removeall
A = D. Items get entries
For I = 0 to D. Count-1 'Repeat Array
Print a (I) 'print entries
Next

Arrkeys = D. Keys
For each objitem in arritems
X = arritems (objitem)
Next


1. dictionary object
When a key/entry pair is added, if the key already exists, or if a key/entry pair is deleted, the key/entry Pair does not exist, or changing the comparemode of a dictionary object that contains data will produce an error.

Attribute
Comparemode setting or string comparison mode of the Return key
Count read-only. Returns the number of key/entry pairs in the dictionary-starting from 1, rather than counting the array from 0.
Item (key) sets or returns the value of the specified key
Key (key) set the key name value

Method
Add (Key, item) to add a key/entry pair to Dictionary
Exists (key) if the specified key exists, true is returned; otherwise, false is returned.
Remove (key) deletes a specified key/entry pair
Removeall () delete all key/entry Pairs
Items () returns an array containing all entries in the dictionary object
Keys () returns an array containing all keys in the dictionary object.

2. add and delete entries in dictionary
Once a new (empty) dictionary is obtained, you can add an entry to it to obtain the entry and delete the entry:

Blnisthere = D. exists ("key1 ")
Stritem = D. Item ("key2 ")
Stritem = D. Remove ("key3 ")
A. removeall

3. Modify the key or entry value
You can modify the data stored in a dictionary by modifying the key value or the data of the entries associated with the specified key. The following code changes the data in an entry whose key is mykey.
A. Item ("mykey") = "newvalue"

If the specified key is not found in the dictionary, a new key/entry pair with mykey as the key and new value as the entry value will be created at the end of the dictionary.
Interestingly, if you use a key that does not exist to retrieve an entry, you will not only get an empty string (this can be thought ), in addition, a new key/entry pair is added to the dictionary. The key is the specified key, but the data of the entry is empty.
You can use the key attribute to only change the value of the key name without changing the data of the corresponding entry. Change an existing key mykey to mynewkey. You can use:
A. Key ("mykey") = "mynewvalue"
If the specified key is not found, a runtime error occurs.

4. Set the comparison Mode
The comparemode attribute of dictionary.
You can specify a comparison method when comparing string keys. The two allowed values are binarycompare (0) and textcompare (1 ). Binarycompare (0) is a binary comparison (case sensitive); textcompare (1) is a text comparison (that is, Case Insensitive ).

5. Traverse dictionary
When studying dictionary, there are two methods and one attribute that requires special attention. They allow us to traverse all key/entry pairs stored in dictionary.
The items method returns all the entry data in the dictionary in the form of a one-dimensional array.
The keys method returns all existing key values with a one-dimensional array.
You can use the Count attribute to obtain the number of keys or entries.
For example, you can use the following code to obtain all key and entry values in a dictionary named D. Note that although the Count attribute stores the number of keys/entries in the dictionary
The subscript of the array should be from 0 to count-1.

Arrkeys = D. Keys 'get all the keys into an array
Arritems = D. Items 'get all the items into an array

For intloop = 0 to D. Count-1 'iterate through the Array
Strthiskey = arrkeys (intloop) 'This is the key value
Strthisitem = arritems (intloop) 'This is the item (data) Value
Next

You can also use for each... The next statement completes the same function:
Arrkeys = D. Keys
For each objitem in arritems
X = arritems (objitem)
Next

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.