enumerator

How do you design an enumerator that returns (theoretically) an infinite amount of items?

I'm writing code that looks similar to this: public IEnumerable<T> Unfold<T>(this T seed) { while (true) { yield return [next (T)object in custom sequence]; } } Obviously, this method is never going to return. (The C# compiler silently allows this, while R# gives me the warning "Function never returns".) Generally...

C# String enums.

Hi, I have the following enumerator: public enum AuthenticationMethod { FORMS = 1, WINDOWSAUTHENTICATION = 2, SINGLESIGNON = 3 } The problem however is that I need the word "FORMS" when I ask for AuthenticationMethod.FORMS and not the id 1. I have found the following solution for this problem: First I need to create a custom at...

for_each on a COM IEnumXxx interface?

I've got a COM object that returns an IEnumUnknown. Is there anything out there that'll turn it into an STL-style iterator? So that I can do something like this: IEnumUnkPtr pEnumUnk; // ...something that fills in pEnumUnk... MagicThing m(pEnumUnk); std::for_each(m.begin(), m.end(), DoSomethingWithUnk); ...or similar? ...

How to iterate over two arrays at once?

I have two arrays built while parsing a text file. The first contains the column names, the second contains the values from the current row. I need to iterate over both lists at once to build a map. Right now I have the following: var currentValues = currentRow.Split(separatorChar); var valueEnumerator = currentValues.GetEnumerator(); ...

How can a formcollection be enumerated in ASP.NET MVC?

How can I enumerate through all the key/values of a FormCollection (system.web.mvc) in ASP.NET MVC? ...

copy n k/v pairs from Hashtable

I have a hashtable with n number of records. I need to copy out the records between x and y and iterate through them. How would I do this? Example: HT1.Count = 500; HT2 = HT1[0] - HT1[100]; --edit-- Just so you are aware, the reasoning for this is I am generating PDF's from .MSG files. The problem arises for the end user that when ...

DataSet: Enumerator and FindById do not return equal DataRow

Hi folks Today's problem in my code is kind of strange, and I could not reproduce it yet. I'm working with a typed dataset (created with the designer) and I'm looping over all rows in a datatable. Sometimes (!), when finding via primary key, the returned row is not equal to the one from the enumerator. This is some code I wrote to repr...

.net enumeration first and last

is there a way in .NET (or some sort of standard extension methods) to ask questions of an enumeration? For example is the current item the first or last item in the enumeration: string s = ""; foreach (var person in PeopleListEnumerator) { if (PeopleListEnumerator.IsFirstItem) s += "["; s += person.ToString(); if (!PeopleLis...

escaping the .each { } iteration early in Ruby

code: c = 0 items.each { |i| puts i.to_s # if c > 9 escape the each iteration early - and do not repeat c++ } I want to grab the first 10 items then leave the "each" loop. What do I replace the commented line with? is there a better approach? something more Ruby idiomatic? ...

Getting the .NET Class associated with a process

As part of a WMI Coupled provider that I'm creating I need to write an instance enumerator. The code I have is below. What I need to do is get the Class instance associated with the process. Any ideas? static public WMIProviderSample GetInstance([ManagementName("ID")] int processId) { try { Process[] ...

what is the pattern for modifying a collection in C#

What is the pattern (best practice) for such problem -- modifying elements (values) in collection? Conditions: size of the collection is not changed (no element is deleted or added) modification is in-place In C++ it was easy and nice, I just iterated trough a collection and changed the elements. But in C# iterating (using enumerato...

Synchronized IEnumerator<T>

I'm putting together a custom SynchronizedCollection<T> class so that I can have a synchronized Observable collection for my WPF application. The synchronization is provided via a ReaderWriterLockSlim, which, for the most part, has been easy to apply. The case I'm having trouble with is how to provide thread-safe enumeration of the col...

Delphi: Using Enumerators to filter TList<T: class> by class type?

Okay, this might be confusing. What I'm trying to do is use an enumerator to only return certain items in a generic list based on class type. Given the following hierarchy: type TShapeClass = class of TShape; TShape = class(TObject) private FId: Integer; public function ToString: string; override; ...

Enumerator problem, Any way to avoid two loops?

I have a third party api, which has a class that returns an enumerator for different items in the class. I need to remove an item in that enumerator, so I cannot use "for each". Only option I can think of is to get the count by iterating over the enum and then run a normal for loop to remove the items. Anyone know of a way to avoid the...

Better way to implement filtered enumerator on TList<TMyObject>

Using Delphi 2010, let's say I've got a class declared like this: TMyList = TList<TMyObject> For this list Delphi kindly provides us with an enumerator, so we can write this: var L:TMyList; E:TMyObject; begin for E in L do ; end; The trouble is, I'd like to write this: var L:TMyList; E:TMyObject; begin for E in L.GetEn...

Ruby Enumeration and RETURN_ENUMERATOR - Questions Regarding Ruby's C Internals

I'm a bit confused about how Ruby handles the creation of Enumerators. Block-based iteration makes sense and is working for me; I am still confused how the return of an Enumerator is supposed to function code-wise. Here is the code I am working with: VALUE rb_RPRuby_Sender_Kernel_each_backtrace_frame( int argc, ...

Ruby Enumerator - Why Finish with Exception?

Iterating a block in Ruby is simple enough - it finishes cleanly and proceeds on to the rest of the code. Iterating with an Enumerator, on the other hand, is a bit more confusing. If you call :each without a block, an Enumerator is returned instead. :next can then be called on the Enumerator to get each next iterative value. And then...

Next key in C# Dictionary

How to get an Enumerator to an item in a -Sorted- dictionary using key? Note:GetEnumerator() gets an Enumerator to first element.. But I need to get an Enumerator to the element with a given key in order to gain access to next elements using MoveNext() for example... Edit: Or a way to access next elements... Edit: I prefer a const ti...

Get next N elements from enumerable.

Context: C# 3.0, .Net 3.5 Suppose I have a method that generates random numbers (forever): private static IEnumerable<int> RandomNumberGenerator() { while (true) yield return GenerateRandomNumber(0, 100); } I need to group those numbers in groups of 10, so I would like something like: foreach (IEnumerable<int> group in RandomNu...

Is there a way to iterate through HttpServletRequest.getAttributeNames() more than once?

I'm trying to log the contents of the HttpServletRequest attributes collection. I need to do this when the servlet first starts, and again right before the servlet is finished. I'm doing this in an attempt to understand a crufty and ill-maintained servlet. Because I need to have as little impact as possible, servlet filters are not an...