Asif
Asif

Reputation: 184

Passing Lambda expression to a method C#

Could you please suggest how can I implement the "Alexa" class?

var alexa = new Alexa();
        Console.WriteLine(alexa.Talk()); //print hello, i am Alexa
        alexa.Configure(x =>
        {
            x.GreetingMessage = "Hello { OwnerName}, I am at your service";
            x.OwnerName = "Bob Marley";
        });
        Console.WriteLine(alexa.Talk()); //print Hello Bob Marley, I am at your service

I tried to implement the method as:

public void Configure(Action<Configration> p)
    {
        
    }

public class Configration
{
    public string GreetingMessage { get; set; }
    public string OwnerName { get; set; }
}

But I can't figure out how to prin the GreetingMessage.

Thanks

Upvotes: 0

Views: 93

Answers (2)

Monofuse
Monofuse

Reputation: 827

If you have access to the talk method, just pass the configuration to the action. Then grab the information from the class you passed to the action.

public class Configration
{
    public string GreetingMessage { get; set; }
    public string OwnerName { get; set; }
}

public class Alexa()
{
    public Configration ConfigurationModel { get; set; }
    public Action<Configration> Configration { get; set; }

    public void Talk()
    {
        this.Configuration.Invoke(ConfigurationModel);
        Console.WriteLine(ConfigurationModel.GreetingMessage);
    }
}

void main()
{
    var alexa = new Alexa();
    Console.WriteLine(alexa.Talk()); //print hello, i am Alexa
    alexa.Configure(x =>
    {
        x.GreetingMessage = "Hello { OwnerName}, I am at your service";
        x.OwnerName = "Bob Marley";
    });
    Console.WriteLine(alexa.Talk()); //print Hello Bob Marley, I am at your service
}

Upvotes: -1

John H
John H

Reputation: 14640

You can do that like so:

public class Alexa
{
    private Configration _configration;

    public Alexa()
    {
        _configration = new Configration
        {
            // This sets the default message you wanted.
            GreetingMessage = "Hello, I am Alexa."
        };
    }

    public void Configure(Action<Configration> configuration)
    {
        // I'll explain this below.
        configuration(_configration);
    }

    public string Talk()
    {
        return _configration.GreetingMessage;
    }
}

But, you do need to change how you call this to get the result you're after. When you do this:

alexa.Configure(x =>
{
    x.GreetingMessage = $"Hello {x.OwnerName}, I am at your service";
    x.OwnerName = "Bob Marley";
});

it will actually print Hello , I am at your service instead of the intended Hello Bob Marley, I am at your service. This is because OwnerName hasn't yet been set to Bob Marley. To fix that, we need to change the assignment to OwnerName to come before it's used in the greeting, like so:

alexa.Configure(x =>
{
    x.OwnerName = "Bob Marley";
    x.GreetingMessage = $"Hello {x.OwnerName}, I am at your service";
});

Finally, you may wonder what configuration(_configration); is doing. It's actually short-hand for configuration.Invoke(_configration);, which will execute the Action delegate, passing _configration as the Configuration parameter.

Calling it using the following:

static void Main(string[] args)
{
    var alexa = new Alexa();
    Console.WriteLine(alexa.Talk());
    alexa.Configure(x =>
    {
        x.OwnerName = "Bob Marley";
        x.GreetingMessage = $"Hello {x.OwnerName}, I am at your service";
    });
    Console.WriteLine(alexa.Talk());
}

Results in:

Hello, I am Alexa.
Hello Bob Marley, I am at your service

Upvotes: 2

Related Questions