1

How to get the value inside if else in a waterfall step dialog and pass it to the next step? Please refer to the code below thank you. Any help is appreciated thank you.

UPDATE: Choosing "Near me" is working fine but when choosing "Somewhere Else" it's getting an error.

 AddStep(async (stepContext, cancellationToken) =>
        {
            var realEstateType = stepContext.Result as FoundChoice;
            var state = await (stepContext.Context.TurnState["FPBotAccessors"] as FPBotAccessors).FPBotStateAccessor.GetAsync(stepContext.Context);
            state.RealEstateType = realEstateType.Value;

            return await stepContext.PromptAsync("choicePrompt",
               new PromptOptions
               {
                   Prompt = stepContext.Context.Activity.CreateReply("Which location are you considering?"),
                   Choices = new[] {new Choice {Value = "Near me"},
                                    new Choice {Value = "Somewhere else"}
                   }.ToList()
               });

        });

        AddStep(async (stepContext, cancellationToken) => 
        {
            var nearOrSomewhereElse = stepContext.Result as FoundChoice;
            var state = await (stepContext.Context.TurnState["FPBotAccessors"] as FPBotAccessors).FPBotStateAccessor.GetAsync(stepContext.Context);
            state.NearOrSomewhereElse = nearOrSomewhereElse.Value;

            var value = "";

            if (state.NearOrSomewhereElse == "Somewhere else")
            {
                await stepContext.PromptAsync("textPrompt",
                new PromptOptions
                {
                    Prompt = stepContext.Context.Activity.CreateReply("Which location are you considering?")
                });

                value = stepContext.Result as string; // i think this is the error. How can i get the result of the block of code inside this if block?
            }
            else if (state.NearOrSomewhereElse == "Near me")
            {
                value = "Near me";
            }

            return await stepContext.NextAsync(value, cancellationToken);

        });



        AddStep(async (stepContext, cancellationToken) =>
        {
            var nearOrSomewhereElse = stepContext.Result as string;
            var state = await (stepContext.Context.TurnState["FPBotAccessors"] as FPBotAccessors).FPBotStateAccessor.GetAsync(stepContext.Context);
            state.NearOrSomewhereElse = nearOrSomewhereElse;

            return await stepContext.PromptAsync("choicePrompt",
             new PromptOptions
                 {
                   Prompt = stepContext.Context.Activity.CreateReply($"Please indicate the size of {state.RealEstateType}"),
                   Choices = new[] {new Choice {Value = "Size 1"},
                                    new Choice {Value = "Size 2"},
                                    new Choice {Value = "Size 3"}
                   }.ToList()
             });                    

        });
enter code here
user10860402
  • 912
  • 1
  • 10
  • 34
  • Possible duplicate of: https://stackoverflow.com/questions/52885068/how-to-pass-value-from-one-waterfalldialog-to-another-waterfalldialog-in-compone – JJ_Wailes Jan 03 '19 at 17:30

1 Answers1

0

The easiest way to do this is using the WaterfallStepContext::NextAsync API passing the value you want to fall through to the next step which will then be accessible to that next step via the WaterfallStepContext::Result property.

That would look something like this:

   // Your first step elided for brevity

   AddStep(async (stepContext, cancellationToken) =>
   {
            var nearOrSomewhereElse = stepContext.Result as FoundChoice;
            var state = await (stepContext.Context.TurnState["FPBotAccessors"] as FPBotAccessors).FPBotStateAccessor.GetAsync(stepContext.Context);
            state.NearOrSomewhereElse = nearOrSomewhereElse.Value;

            if (state.NearOrSomewhereElse == "Near me")
            {
               value = "Near me";
            }
            else if (state.NearOrSomewhereElse == "Somewhere else")
            {
               //prompt user. user's answer will be stored to value.
                value = "User input";
            }

            // Call NextAsync passing on the value
            return await stepContext.NextAsync(value, cancellationToken);
   });

   AddStep(async (stepContext, cancellationToken) =>
   {
         // Retrieve the result of the previous step
         var x = stepContext.Result as string;

         // … use the value here …
   });
Drew Marsh
  • 33,111
  • 3
  • 82
  • 100
  • Sir i updated the code can you look at it again. Thank you – user10860402 Jan 04 '19 at 02:44
  • 1
    Well, for the record, this isn't really how StackOverflow works... it's not an iterative code review session. If you want more help tho I'll at least need the details of the exception you're getting. – Drew Marsh Jan 04 '19 at 03:10
  • Thanks sir i managed to fix thank you very much. I was hoping you can teach me how to simplify this waterfall property accessor? i have no idea about that. – user10860402 Jan 04 '19 at 04:48
  • Oh good, glad to hear it. If you would like some help with removing the property accessor, please feel free to open a new question and I'll gladly help out! – Drew Marsh Jan 04 '19 at 17:01
  • Here is the question sir. https://stackoverflow.com/questions/54058817/botframework-v4-how-to-simplify-this-waterfall-dialog – user10860402 Jan 06 '19 at 05:09