Home > Software engineering >  Why use VBA dictionary function, the corresponding key content is the same?
Why use VBA dictionary function, the corresponding key content is the same?

Time:11-14

 
Public Sub TEST ()
The Set RECORD_DIC=CreateObject (" Scripting. The Dictionary ")
The Set RECORD_INDEX=CreateObject (" Scripting. The Dictionary ")
RECORD_DIC (" id ")=111
RECORD_DIC (" content ")=5678

RECORD_INDEX. Add "a", RECORD_DIC

RECORD_DIC (" id ")=222
RECORD_DIC (" content ")=7890

RECORD_INDEX. Add "b", RECORD_DIC

The Debug. Print RECORD_INDEX. Count
The Debug. Print RECORD_INDEX (" a ") (" number ")
The Debug. Print RECORD_INDEX (" b ") (" number ")
End Sub
"' output is 222, and, what's the matter?
"' RECORD_INDEX (" a") (" code ") and RECORD_INDEX (" a ") (" number ") why output is' 222 '??

CodePudding user response:

Should be used like this:
 
Public Sub TEST ()
The Set RECORD_INDEX=CreateObject (" Scripting. The Dictionary ")

The Set RECORD_DIC=CreateObject (" Scripting. The Dictionary ")
RECORD_DIC (" id ")=111
RECORD_DIC (" content ")=5678

RECORD_INDEX. Add "a", RECORD_DIC

The Set RECORD_DIC=CreateObject (" Scripting. The Dictionary ")
RECORD_DIC (" id ")=222
RECORD_DIC (" content ")=7890

RECORD_INDEX. Add "b", RECORD_DIC

The Debug. Print RECORD_INDEX. Count
The Debug. Print RECORD_INDEX (" a ") (" number ")
The Debug. Print RECORD_INDEX (" b ") (" number ")
End Sub

CodePudding user response:

Because you have a problem understanding of dictionary, take up a Chinese dictionary, by looking for any Chinese characters (such as "after"), you can find the detailed definition of the word, the Chinese character is called the index and its corresponding detailed explanation is the value of indexed,
Take a look at your code, RECOD_DIC (" key ")=value represents the create (edit) if already exists, a key for the index, the value for the index value dictionary; The result is
 
Manually add a called "edit" index, index value is 111;
Manually add a call "content" of the index, the index value is 5678.
Edit a called "edit" index, index value is 222;
Edit a call "content" of the index, the index value is 7890,

Understand where the problem?
There are two other way:
1. According to the thinking of you now, maintain two dictionary, "INDEX - & gt; Number "map," number - & gt; Content "map
 RECORD_DIC (111)=5678 
RECORD_INDEX (" a ")=111
RECORD_DIC (222)=7890
RECORD_INDEX (" b ")=222
'values
The Debug. Print RECORD_DIC (RECORD_INDEX (" a "))
The Debug. Print RECORD_DIC (RECORD_INDEX (" b "))


2. Use a dictionary, index with "a", "b", the index value to save a struct or class objects

CodePudding user response:

reference 1st floor milaoshu1020 response:
should be used like this:
 
Public Sub TEST ()
The Set RECORD_INDEX=CreateObject (" Scripting. The Dictionary ")

The Set RECORD_DIC=CreateObject (" Scripting. The Dictionary ")
RECORD_DIC (" id ")=111
RECORD_DIC (" content ")=5678

RECORD_INDEX. Add "a", RECORD_DIC

The Set RECORD_DIC=CreateObject (" Scripting. The Dictionary ")
RECORD_DIC (" id ")=222
RECORD_DIC (" content ")=7890

RECORD_INDEX. Add "b", RECORD_DIC

The Debug. Print RECORD_INDEX. Count
The Debug. Print RECORD_INDEX (" a ") (" number ")
The Debug. Print RECORD_INDEX (" b ") (" number ")
End Sub

This writing is not very reasonable, each item in the RECORD_INDEX kept a dictionary, this is only one actually have to do is save a item,

CodePudding user response:

reference X - I - n reply: 3/f
Quote: refer to 1st floor milaoshu1020 response:

Should be used like this:
 
Public Sub TEST ()
The Set RECORD_INDEX=CreateObject (" Scripting. The Dictionary ")

The Set RECORD_DIC=CreateObject (" Scripting. The Dictionary ")
RECORD_DIC (" id ")=111
RECORD_DIC (" content ")=5678

RECORD_INDEX. Add "a", RECORD_DIC

The Set RECORD_DIC=CreateObject (" Scripting. The Dictionary ")
RECORD_DIC (" id ")=222
RECORD_DIC (" content ")=7890

RECORD_INDEX. Add "b", RECORD_DIC

The Debug. Print RECORD_INDEX. Count
The Debug. Print RECORD_INDEX (" a ") (" number ")
The Debug. Print RECORD_INDEX (" b ") (" number ")
End Sub

This writing is not very reasonable, each item in the RECORD_INDEX kept a dictionary, this is only one actually have to do is save a item,

Just work, existence is reasonable;
  •  Tags:  
  • VBA
  • Related