Solved

vb.net remove object

Posted on 2014-09-29
6
291 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

688 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