Technorati label:VBA,Variable Declaration
Today, I made another call.
I declare an annatree dynamic array. The annatree class has a member variable of the Treeview class. I want to add a node under a tree in this dynamic array and assign a value to the tag of this node.
- Dim objohtersameatree () as annatree
- Set objohternode = objohtersameatree (I). Tree. nodes. Add (relative: = m_tree.selecteditem.key, relationship: = tvwchild, text: = objnewnode. text, image: = 2)
- Objothernode. Tag = ""
As shown in the preceding figure, when F8 is reached the last line, an error is reported, "the object or with block variable is not set ". In fact, the monitoring window shows that objothernode is normal, and its tag can also be seen. However, once objothernode. Tag is directly monitored, "the object or with block variable is not set ". Later I changed it to this, but it was right:
- Dim objohtersameatree () as annatree
- Dim treothersame as mscomctllib. Treeview
- Set treothersame = objohtersameatree (I). Tree
- Set objothernode = treothersame. nodes. Add (relative: = m_tree.selecteditem.key, relationship: = tvwchild, text: = objnewnode. text, image: = 2)
- Objothernode. Tag = ""
Just put the tree in the array out, and then click it. I was thinking, it is estimated that VB also has its own bug. This kind of logic-compliant but not-conforming super usage like me, it is estimated that their testers could not think of testing, so, I had to swallow this bitter fruit myself ...... Similarly, I also met one time. See the following:
- Strparentkey = m_tree.nodes.item (objnode. Key). Parent. Key
When this line is run, the object or with block variable is not assigned a value.
- Set objrelatednode = m_tree.nodes.item (objnode. Key)
- Strparentkey = objrelatednode. Parent. Key
It is passed. It may be that a reference that is too long has caused compilation ambiguity.
Summary:A reference that is too long can be turned around with an intermediate variable. Otherwise, VBA may not be able to understand it ......