Home > Software engineering >  VB Dictionary object Scription. Dictionary of internal storage array read-only
VB Dictionary object Scription. Dictionary of internal storage array read-only

Time:10-02

Forget to have encountered such a problem before

Describe the problem first, use a dictionary in the object storage array to
1. Use a dictionary in the object element can pass the KEY value index, rather than an array of digital index value,
2. In the dictionary object dictionary object, of course, that's right, from the cost into consideration, to switch to storage array

But cannot change the storage array, array values in the dictionary object, became a read-only, wonder, also please advise the solution and what is the nature of the problem,
VB code

 
Dim test As New Scripting. The Dictionary
Dim b (1) As an Integer
B (0)=100
B=20 (1)
Test. The Add "a1", b
MsgBox (test Item (" a1 ") (1)) 'shows that there is nothing wrong with 20

The test Item (" a1 ") (1)=444
MsgBox (test Item (" a1 ") (1)) 'expectation is 444, but the actual display or 20

CodePudding user response:

B (1)=444
Test (" a1 ")=b
Msgbox test (" a1 ") (1)

CodePudding user response:

The upstairs method can solve the problem of direct assignment result in invalid
But is not art because

I'll keep this dictionary in the actual application objects by reference to other functions or methods, the array b should be rid of garbage collection, so is passed to the function or method cannot directly write
B (1)=444
But must declare a temporary array variable such as
Dim c (1) as an integer
Then there is the
C (0)=test. The item (" a1 ") (0)
C (1)=444
The test item (" a1 ")=c
But when more amount of elements in the array will be very troublesome,

Of course is a kind of solution, so I said this is not art,

More want to know the causes of this problem, to grasp the essence or method is naturally thought of,
Is the element of a dictionary object if is directly measured value, is the relationship between the memory address, but also can't give the reference sense after assignment, seems to be more should be
The test item (" a1 ") (... ) the problem in this writing,

CodePudding user response:

You try this:
 ' '... 
Dim c
C=test (" a1 ")
C (1)=444
Test (" a1 ")=c
Msgbox test (" a1 ") (1)
"'...



CodePudding user response:

Thank you reply but upstairs
Kiss I'm sorry can only say that your code is not tested
Display value is still the assignment before the original value of the
The test code below to
 
Private Sub Form_Load ()
Dim mydic As New Scripting. The Dictionary
The Set mydic=test ()
MsgBox (mydic. Item (" ok ") (1)) 'original value shown here has 2 yes

Dim cc
Cc=mydic. Item (" ok ")
Cc (1)=44
MsgBox (cc) (1) 'show cc (1) for 44 also right here
MsgBox (cc (0)) 'cc (0) is 1 is shown here also right

MsgBox (mydic. Item (" ok ") (1)), 'here is original value of 2 is not hope
End Sub

Private Function test () As Scripting. The Dictionary
Set the test=New Scripting. The Dictionary

Dim a (2) As an Integer
A (0)=1
A (1)=2
A (2)=3

Test. The Add "ok", a
End the Function

Direct assignment seems to be a regular array by value, so the above methods do you use the dictionary object ok elements stored in the array is assigned to the cc by value, so cc change cannot cause a dictionary object change in the value of the corresponding element

CodePudding user response:

You have a good look at # 3 code # 4 floor with you,,,, look carefully... The comparison of one line

CodePudding user response:

 test. The Item (" a1 ") (1)=444 

This statement actually should be open to see
 dim v as the variant 
V=test. The Item (" a1 ") key='to get the collection of a1 members'
V (1)=444 'members if the return is an array, members of the subscript 1 assignment 444'

See v this VARIANT structure of memory, will find that there is no sign VT_BYREF, instructions for each test. The Item (" a1 ") when the members of the returned array is the array to copy a return on the whole, so to change does not affect the members of the returned array,

If you want to use the test Item (" a1 ") (1)=444 members modify the contents of the array, you need to use a Class to encapsulate the array b (), and the Item (index) attribute set as the default,

CodePudding user response:

3 # and 4 # code is less item keywords, general personal habits, in order not to follow an array or other visual confusion, so a dictionary object at the time of call key value will be written as
Dic. Item (" XXXX ")

Don't mean to blame irresponsible upstairs, ha ha, let you misunderstood, I'm sorry, just relax!
Hardly the two of us to a small detail discussion to the 7 #

Seem to be associated with storage and access methods, the class package I don't quite understand you say, whether through this kind of packaging can make originally the value assignment mandatory for reference, can give a simple example will thank humbly

CodePudding user response:

The last time, you can test the code below:
 Sub test () 
Dim iDic As New Dictionary, and b (1) As an Integer, c
B (0)=120
B (1)=300
IDic (" a1 ")=b
C=iDic (" a1 ")
C (1)=444
IDic (" a1 ")=c
MsgBox iDic (" a1 ") (1) "' this is 444
End Sub


CodePudding user response:

Er really neglect, forgive my carelessness, array change after did not return to a dictionary object
Tried under is feasible and relatively simple than the first one

CodePudding user response:

Class1
 '
Option Explicit

Private b (1) As an Integer

Public Property Get Item (ByVal Index As Long) As an Integer
The Attribute Item. VB_UserMemId=0
Let the Item=b (Index)
End Property

Public Property Let Item (ByVal Index As Long, ByVal RHS As Integer)
Let b (Index)=RHS
End Property

 Dim test As New Scripting. The Dictionary 
Dim c As New Class1
C (0)=100
C=20 (1)
Test. The Add "a1", c
MsgBox (test Item (" a1 ") (1))

The test Item (" a1 ") (1)=444
MsgBox (test Item (" a1 ") (1))

Note: the Attribute statements are automatically added after set the default attributes, only in Class1. CLS file can be seen,
  • Related