?
Solved

vb.net remove object

Posted on 2014-09-29
6
Medium Priority
?
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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 2000 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month9 days, 5 hours left to enroll

765 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