views:

347

answers:

4

I have a class that takes an action in it's constructor.

Example:

public CustomClass(Action<Path> insert)
{

  // logic here...

}

I currently instantiate this class using the following line of code:

var custom = new CustomClass((o) => LayoutRoot.Children.Add(o));

I want to modify the custom class to include an additional constructor, such as the following:

public CustomClass(Action<Path, TextBlock> insert)
{

  // logic here...

}

However, my knowledge of lambda expressions is pretty basic, so I can't figure out how to instantiate the custom class, passing two parameters in the action to the new constructor.

Any help would be greatly appreciated.

Thanks.

+4  A: 

In Lamba you can pass two parameters as such:

(x, y) => { x.DoSomething(); y.DoSomethingElse(); }
Tony
yes, but you should also show him how to change his constructor to be able to accept that `Action`
Stan R.
I believe if it's a compound statement, you'll need curly braces around the body: (x, y) => { x.DoSomething(); y.DoSomethingElse(); }
Dathan
Are there a missing pair of { } here?
AnthonyWJones
@Stan R. The OP already provided the modified constructor...
Dathan
@Dathan, sorry it was just edited, it wasn't there when the question was originally asked.
Stan R.
Ha! @Tony you beated me for a few seconds :P but I didnt forget the {}'s! and he didnt had the right constructor originally ;)
Francisco Noriega
+1  A: 

Mmm first, both the constructors you wrote are the same.... but i think i get it.

In order to pass 2 parameters to the action, just define the insert action as an Action<T,T2> and when you call it do it like this:

var custom = new CustomClass((o,u) => {LayoutRoot.Children.Add(o); somethingElse(u)});
Francisco Noriega
Great - thank you very much!
Chris
Both constructors weren't meant to be the same - I forgot to format my code snippets as 'code', so when I posted the original question, somehow the constructors appeared identical, so I edited the post to format the snippets as code, and now the constructors should be different.Anyway, your answer worked, so thanks again!
Chris
Im glad it helped.. could you mark as answer and/or vote up? Thanks!
Francisco Noriega
A: 

Either you are asking

public CustomClass(Action insert, Action insert2) { // logic here... }

or

 public CustomClass(Action<T1, T2> insert) { // logic here... }

The second constructor will take an delegate that receives 2 parameters. So you can do something like

CustomClass class = new CustomClass( (x,y) => DoSomething(x,y) );
Stan R.
A: 

You can create a lambda expression that takes more than one parameter by surrounding the parameter list with parentheses and comma separate the parameters:

var custom = new CustomClass((o, tb) => /* use both params somehow */ );

If you need to perform more than one statement in a lambda, you can surround the body of the lambda with braces:

var custom = new CustomClass((o, tb) => { o.DoSomething(); tb.DoSomethingElse() } );

You can learn more about lambda syntax here on MSDN.

LBushkin