treenode bug on Remove()?

  • Thread starter Thread starter Eric Newton
  • Start date Start date
E

Eric Newton

arg...

the following code calling node.Remove() gives NullReferenceException: and
PS the Node is already added to a TreeView... I promise!

Private Shared Sub ClearFieldNodes(ByVal rootNode As TreeNode)
For Each node As TreeNode In rootNode.Nodes
If TypeOf node.Tag Is Class1 Then node.Parent.Nodes.Remove(node)
Next
End Sub

[bug] or [feature]
^^^^
 
sorry guys, i'm on a roll today... new WORKAROUND code for silly framework
bug...

Private Shared Sub ClearFieldNodes(ByVal rootNode As TreeNode)
'EN this errors... WHY? why? why? too many workarounds
'For Each node As TreeNode In rootNode.Nodes
' If TypeOf node.Tag Is APTIPARSE.CField Then
node.Parent.Nodes.Remove(node)
'Next
Dim nodes As New ArrayList
For Each node As TreeNode In rootNode.Nodes
If Not (TypeOf node.Tag Is APTIPARSE.CField) Then nodes.Add(node)
Next
rootNode.Nodes.Clear()
For Each node As TreeNode In nodes
rootNode.Nodes.Add(node)
Next
End Sub

its silly... its messy... its arcane... but hey... it works... in an
overzealous and bloated kind of way...
 
It may not be a bug.... it may be an issue with the remove.... for example
if there are 5 items, you remove 3rd, now 5th item is null so when you reach
the 5th item index, it throws a null pointer exception. You can track it
down by,
1) Try checking the number of nodes after each removal.
2) Try running your own loop from max_nuber to 0 in reverse order

I am sure you will be able to track it down, If its really a bug, we will
whinge together ;)

I remember having seen this issue in the collections previously (C++ not VB
though) and I had to write code which used to run something like

for (long lIndex = 1; lIndex <= Collection->GetCount();) //notice the
increment is not here
{
<Get item from collection> ;
<Check for the condition, if its met>
{
<remove the item>
continue; //continue without incrementing the iterator
}

lIndex++; //increment the iterator only if we have not removed the item.
}


HTH,

--Saurabh

Eric Newton said:
sorry guys, i'm on a roll today... new WORKAROUND code for silly framework
bug...

Private Shared Sub ClearFieldNodes(ByVal rootNode As TreeNode)
'EN this errors... WHY? why? why? too many workarounds
'For Each node As TreeNode In rootNode.Nodes
' If TypeOf node.Tag Is APTIPARSE.CField Then
node.Parent.Nodes.Remove(node)
'Next
Dim nodes As New ArrayList
For Each node As TreeNode In rootNode.Nodes
If Not (TypeOf node.Tag Is APTIPARSE.CField) Then nodes.Add(node)
Next
rootNode.Nodes.Clear()
For Each node As TreeNode In nodes
rootNode.Nodes.Add(node)
Next
End Sub

its silly... its messy... its arcane... but hey... it works... in an
overzealous and bloated kind of way...


Eric Newton said:
arg...

the following code calling node.Remove() gives NullReferenceException: and
PS the Node is already added to a TreeView... I promise!

Private Shared Sub ClearFieldNodes(ByVal rootNode As TreeNode)
For Each node As TreeNode In rootNode.Nodes
If TypeOf node.Tag Is Class1 Then node.Parent.Nodes.Remove(node)
Next
End Sub

[bug] or [feature]
^^^^

--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
(e-mail address removed)-software.com [remove the first "CC."]
 
to my understanding you are not supposed to delete a member which is under
consideration(node in your case) in ForEach loop
try For loop...
 
Back
Top