I Love Stackoverflow
I Love Stackoverflow

Reputation: 6868

Argument error while passing Action delegate as parameter to constructor

I am trying to pass Action delegate with to constructor but getting below error :

Delegate 'Action' does not take 0 arguments

Code :

public sealed class VariantProcessor
{
    private string _myAppConnectionString { get; set; }
    private readonly Action<Variant> _transform;
    public Variant(string _myAppConnectionString,Action<Variant> transform)
    {
         _myAppConnectionString = _myAppConnectionString;
         _transform = transform;
    }
    public void Process(Variant model)
    {
        try
        {
            _transform(model);
             //version creation shared by both the derived types
        }
        catch (Exception) 
        {
        }
    }
}

public class AggregateCalculator : IVariantProcessor
{
    private string _myAppConnectionString { get; set; }
     public void Process(Variant model)
     {
            _myAppConnectionString = ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;
            new VariantProcessor( _myAppConnectionString,
               () => Transform(model) //error
               );
     }
    private void Transform(Variant model)
    {
        //logic on variant model
    }
}

I tried like this as well but still no luck :

new VariantProcessor(_myAppConnectionString,
                   Transform(model) // error
                    );

Actually I have problem understanding this syntax () => Transform(model) hence I am not getting whats the problem here.

Can someone please help me figuring out whats the problem here ?

Upvotes: 0

Views: 426

Answers (2)

Jaydeep Jadav
Jaydeep Jadav

Reputation: 836

Try to use the following code. You requires to pass an argument in lemda

public class AggregateCalculator : IVariantProcessor
{
    private string _myAppConnectionString { get; set; }
     public void Process(Variant model)
     {
            _myAppConnectionString = ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;
            new VariantProcessor( _myAppConnectionString,
               (o) => Transform(model)
               );
     }
    private void Transform(Variant model)
    {
        //logic on variant model
    }
}

Upvotes: 1

Sean
Sean

Reputation: 62472

The Action that your constructor takes is a Action<Variant> which means it is a piece of code that takes a single parameter of type Variant and returns void (because an Action doesn't return anything.

When you call the constructor you can either pass it a lambda that takes a single parameter like this:

new VariantProcessor(_myAppConnectionString, model => Transform(model));

or you can pass it the name of a function that takes a Variant and returns void:

new VariantProcessor(_myAppConnectionString, Transform);

The syntax:

() => Transform(model)

means a lambda that takes zero parameters (that's that the ()) means. You need to supply a lambda that takes a single parameter. You can write it either as:

model => Transform(model)

or

(model) => Transform(model)

When you've got a lambda that takes more than one parameter then you have to put them inside brackets.

Upvotes: 3

Related Questions