Solved

vb.net remove object

Posted on 2014-09-29
6
287 Views
Last Modified: 2014-09-29
The below loop appears to remove the objects "MyNote" that match the criteria because they disappear from the form, but they remain due to the numbers still show up in any math performed on the collection of objects.  An example of the math is the next loop.  How can I remove the objects "MyNote" from memory in addition to removing from the form so they no longer show up in the loop that performs math on the collection?

For Each MyNote As RegisterSingleLine In col
   If MyNote.CmdHighLight.BackColor = Color.Yellow Then
   Me.SplitContainer1.Panel2.Controls.Remove(MyNote)
  End If
Next MyNote

LblTOTAL.Text = 0
For Each MyNote As RegisterSingleLine In col
   LblTOTAL.Text = CInt(LblTOTAL.Text) + CInt(MyNote.TxtPrice.Text)
Next MyNote
0
Comment
Question by:dastaub
  • 3
  • 2
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40349389
you are indeed removing it from the "form", but not from the collection, I do presume that col is not referring to Me.SplitContainer1.Panel2.Controls

you need to also remove the object from the collection, but if you do this:
For Each MyNote As RegisterSingleLine In col
   If MyNote.CmdHighLight.BackColor = Color.Yellow Then
     Me.SplitContainer1.Panel2.Controls.Remove(MyNote)
     col.Remove(MyNote)
  End If
Next MyNote

Open in new window

you will run into this error:
Collection was modified; enumeration operation may not execute.

the alternatives are :
* create another collection that holds the items to be removed, and after you first loop, loop on that collection to remove the items from the original collection
* use a FOR loop on the index positions (from last to first item using STEP -1) instead of FOR EACH
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40349413
Another alternative:

For Each MyNote As RegisterSingleLine In col
   If MyNote.CmdHighLight.BackColor = Color.Yellow Then
   Me.SplitContainer1.Panel2.Controls.Remove(MyNote)
  End If
Next MyNote

LblTOTAL.Text = 0
At this point you loop through the Me.SplitContainer1.Panel2.Controls collection and then perform the totals calculation on the RegisterSingleLine objects in that collection.
0
 

Author Comment

by:dastaub
ID: 40350003
below "Another Alternative", I do not see any change in the code?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40350030
Look again.

Your code : For Each MyNote As RegisterSingleLine In col  (you have some collection called col)
My suggestion:  loop through the Me.SplitContainer1.Panel2.Controls collection  (this uses the inbuilt collection of controls on the form - which you have removed the unwanted controls from)
0
 

Author Comment

by:dastaub
ID: 40350234
I'm trying to see the change, but it's not clear?

For Each MyNote As RegisterSingleLine In col
    If MyNote.CmdHighLight.BackColor = Color.Yellow Then
    Me.SplitContainer1.Panel2.Controls.Remove(MyNote)
   End If
 Next MyNote


For Each MyNote As RegisterSingleLine In col
    If MyNote.CmdHighLight.BackColor = Color.Yellow Then
    Me.SplitContainer1.Panel2.Controls.Remove(MyNote)
   End If
 Next MyNote
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 40350348
I'll repeat my earlier comment:

Another alternative:

For Each MyNote As RegisterSingleLine In col
   If MyNote.CmdHighLight.BackColor = Color.Yellow Then
   Me.SplitContainer1.Panel2.Controls.Remove(MyNote)
  End If
Next MyNote

LblTOTAL.Text = 0
At this point you loop through the Me.SplitContainer1.Panel2.Controls collection and then perform the totals calculation on the RegisterSingleLine objects in that collection.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question