473,394 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Delete from generic list using ForEach

I have a generic list object with a property called,
"MarkedForDeletion". During the course of my processing, some of the
objects in the list will get this property set to true and so at the
end I want to loop through my list and remove any object that has this
property set to true.

Currently I have code like this to accomplish this:

List<MyObjectdetails = MyMethodToGetDetails();
MyMethodToMarkDetailsForDeletion();

List<MyObjectfilteredDetails = details.GetRange(0, details.Count);
MyOjbect currentDetail = null;

for (int deleteIndex = 0; deleteIndex < details.Count; deleteIndex++)
{
currentDetail = details[deleteIndex];
if (currentDetail.MarkedForDeletion)
{
filteredDetails.Remove(currentDetail);
}
}

I want to look at replace this for loop with the ForEach method of the
generic list object. However, the problem is that the Action delegate
of this method does not allow me to pass anything other than the
specific object within the list that I'm dealing with. So I don't
know how to build a delegate that will allow me to remove the object
from within the overall list.

Does anyone have any ideas on how to do this?

Aug 6 '07 #1
3 2684
On Aug 6, 2:20 pm, Doug <dnlwh...@dtgnet.comwrote:
I have a generic list object with a property called,
"MarkedForDeletion". During the course of my processing, some of the
objects in the list will get this property set to true and so at the
end I want to loop through my list and remove any object that has this
property set to true.
In that case, I suggest you use List<T>.RemoveAll with a predicate.

I didn't quite follow your example - if RemoveAll isn't enough for
your needs, could you write a short but complete example which
demonstrates the problem?

See http://pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Jon

Aug 6 '07 #2
In that case, I suggest you use List<T>.RemoveAll with a predicate.

This was exactly what I needed and removes my need for the
"filteredDetails" object. Thank you!

Aug 6 '07 #3
A suggestion: your post was _very_ long for what appears to be a simple
question. You will get better answers if you can ask your questions
more concisely. I know this from personal experience, having some
problems being brief myself. :)
Hi Peter, sorry about that. In a previous post, Jon had suggested
posting my complete code and I tried to shrink it down as much as I
could because long posts can get confusing, but that was the best I
could come up with.

I think you have the gist of what I was looking for in that post. I
didn't put it into a seperate class, but I did have to create a member
level variable like you did in that class in ordre to do my work. I'm
not a big fan of member level variables (it's easy to lose track of
what they should be set to, are they set, etc, etc) but that's what I
went with.

Aug 6 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Adam Clauss | last post by:
OK, I have class A defined as follows: class A { A(Queue<B> queue) { ... } } Now, I then have a subclass of both classes A and B. The subclass of A (SubA), more specifically is passed a...
2
by: Harold Howe | last post by:
Howdy all, I am getting a compiler error regarding a consrained conversion. It complains that it can't make the type conversion, even though the generic type argument inherits from the target of...
4
by: Adam Clauss | last post by:
I ran into a problem a while back when attempting to convert existing .NET 1.1 based code to .NET 2.0 using Generic collections rather than Hashtable, ArrayList, etc. I ran into an issue because...
2
by: Brian Richards | last post by:
I'm trying to write a generic function (List<TPanelType> GetGenericPanels<TPanelType, TObjectType>()) that returns all UserControls that derive from T and and implement an interface...
2
by: MarkAurit | last post by:
Ive been using arraylists in 1.1 to return collections of custom business objects, and thoroughly enjoyed their simple programming style. After hearing of the advantages of generics during a 2.0...
10
by: fig000 | last post by:
HI, I'm new to generics. I've written a simple class to which I'm passing a generic list. I'm able to pass the list and even pass the type of the list so I can use it to traverse it. It's a...
4
by: Tony | last post by:
Hello! Below I have a complete working program.with some simple classes one of these is a generic class. The question is about this method GetCows() {...} which is a member in the generic...
5
by: Bill McCormick | last post by:
Hi C# programmers, I'd like to be able to call a static function or property and have it return a generic list. The compiler indicates that "static types cannot be used as generic arguments", so...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.