0

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

1
  • I don't understand how "Passing Lambda expression to a method C#" is related to suggesting how to implement a class? Commented Sep 19, 2020 at 12:02

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

-1

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.