Solved

vb.net remove object

Posted on 2014-09-29
6
289 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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 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…

749 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