Solved

vb.net remove object

Posted on 2014-09-29
6
281 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 142

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now