-1

I am not that great in c#. But i was trying to understand this code in regards to BOT framework. Here is method

namespace HotelBot.Dialogs
{
[Serializable]
public class GreetingDialog : IDialog
{

    public async Task StartAsync(IDialogContext context)
    {
        await context.PostAsync("I am kiran");
        context.Wait(MessageRecievedAsync);

    }

    public virtual async Task MessageRecievedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)
    {
        var message = await argument;
        var userName = string.Empty;

        context.UserData.TryGetValue<string>("Name", out userName);

        if (string.IsNullOrEmpty(userName))
        {
            await context.PostAsync("What is your name");
            userName = message.Text;
            context.UserData.SetValue<string>("Name", userName);
        }
        else
        {
            await context.PostAsync(string.Format("Hi {0}. How can i help u?" + userName));
        }

        context.Wait(MessageRecievedAsync);

    }
}
}

This line in code calls that method

context.Wait(MessageRecievedAsync); // why no parameters are required

I want to know why parameters are not required for that function to pass?

James
  • 1,827
  • 5
  • 39
  • 69
  • You forgot to tag question properly. What is `IDialogContext`? As for queston, `Wait()` method probably has overload accepting delegate with same type as `MessageReceivedAsync` method is (see [this](http://stackoverflow.com/q/2082615/1997232) question), then all you need is to supply *name of that method* (if that's what confuses you), inside `Wait` method something will call delegate with some parameters, so at the end there **is** `MessageReceivedAsync(context, argument)` call. – Sinatr Mar 08 '17 at 12:27
  • Are you in a derived class where the base-class has a matching overload for that method? – lokusking Mar 08 '17 at 12:42
  • @lokusking no not in derived class – James Mar 08 '17 at 12:43
  • @Sinatr i updated the question and added whole class – James Mar 08 '17 at 12:47

1 Answers1

3

The reason it does not need parameters is the method signature for Wait looks like

void Wait<R>(ResumeAfter<R> resume)

ResumeAfter<R> is defined as

delegate Task ResumeAfter<in T> (IDialogContext context, IAwaitable<T> result)

So it is a delegate. With delegates you can do a shorthand where you exclude specifying the type. the real thing you called was

context.Wait(new ResumeAfter<IMessageActivity>(MessageRecievedAsync));

Which creates passes a reference to the MessageRecievedAsync and lets Wait call the function inside of it. If you are familiar with lambda expressions another way you could write this would be:

context.Wait((IDialogContext context, IAwaitable<IMessageActivity> result) => MessageRecievedAsync(context, result));
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431