How to pass arguments to delegate in Enumerable::Select
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: How to pass arguments to delegate in Enumerable::Select

Hybrid View

  1. #1
    Join Date
    Mar 2013
    Posts
    2

    How to pass arguments to delegate in Enumerable::Select

    Hello gurus!

    Could you please advice how to call delegate with 2 params within Enumerable::Select?

    I have understanding how to call function with 1 param (getId sample below), but didn't managed to pass extra params (isIdInCollection func).
    It's not possible to me to share someIds List as global var for IsIdInCollection , only way is to pass it as argument.

    Code:
    Guid GetId (MyStruct^ s) { return s->Id; }
    bool IsIdInCollection (Guid id, List<Guid>^ collection) { return Enumerable::Contains(collection, id); }
    		
    void Test (List<MyStruct^>^ structCollection, List<Guid>^ someIds)
    {
    	auto getId = gcnew Func<MyStruct^, Guid>(GetId);
    	List<Guid>^ idCollection = Enumerable::ToList(Enumerable::Select(structCollection, getId));
    				
    	auto isIdInCollection = gcnew Func<Guid, List<Guid>^, bool>(IsIdInCollection);
    	List<Guid>^ selectedIdCollection = Enumerable::ToList(Enumerable::Select(idCollection, isIdInCollection {someIds} ));
    }
    Thank you in advance,
    Natalia.

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,571

    Re: How to pass arguments to delegate in Enumerable::Select

    Actually, what you seem to be looking for is a combination of the methods Enumerable::Select() and Enumerable::Where(). You'd then use a two-step process: First extract the IDs, as you're already doing in your first Select() sample, and then filter that list using Where() with an appropriate predicate based on your someIDs. I don't know of any framework method that does that in a single step.

    An alternative approach I see is performing the filtering as a set operation:

    Code:
    SortedSet<Guid> ^setResults = gcnew SortedSet<Guid>(idCollection);
    setResults->IntersectWith(someIDs);
    IMO this approach is likely to be more efficient in terms of performance, and even more performant when using a hash set.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Mar 2013
    Posts
    2

    Re: How to pass arguments to delegate in Enumerable::Select

    Thank you, this solves my problem.

    But what about extra params? is there a way to pass them in cases when there is no possibility to use other functions?

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,571

    Re: How to pass arguments to delegate in Enumerable::Select

    You can create delegates referring to practically any function that can have any combination of parameters you like. However, like with any function call, the delegate's signature must match what the function you pass it to (e.g. Enumerable::Where()) expects, so eventually it's that function that determins the decision for the parameter set. There are alternative overloads of Select() and Where() taking a delegate with an additional int parameter over which they pass the item sequence number to the selector function/predicate, so that can base decisions on it, but that's it.

    Or do you mean parameterizable predicates which make decisions based on information not known until runtime? They can be represented by a class you create yourself and that exposes a predicate delegate member, for instance something like this: http://forums.codeguru.com/showthrea...ated-predicate. In the meantime I became used to using this sort of predicates quite freqently. They can be of arbitrary complexity, mostly limited by imagination...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center