Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello. I was wondering if there was a way to assign certain responses to a case statement. This is an example of what I want to do;
C#
switch(e.result.text)
{
    case "Tell me a joke":
        synthesizer.speakasync("Why did the chicken cross the road?");
        break;
        
        if(case == "I don't know")
            synthesizer.speakasync("to get to the other side");
            break;

        else if(case == " ")
            synthesizer.speakasync("Take a Guess");
            break;
}


Is it possible to do this or not? I've tried a lot of things like making the speech recognition engine a variable and creating new voids to store these responses in but it doesn't seem to be working.
Posted
Comments
Richard Deeming 18-Aug-15 8:29am    
Your switch syntax is wrong - you don't use if or else if for different cases:
switch (C# Reference)[^]

Other than that, it's not entirely clear what you're trying to do.

1 solution

Since you use a string to return another string, using a Dictionary<string, string> would be pretty straightforward, e.g.

C#
Dictionary<string, string> response = new Dictionary<string, string>()
{
  {"Tell me a joke", "Why did the chicken cross the road?"},
  {"I don't know", "to get to the other side"},
  {" ", "Take a Guess"},
};


Console.WriteLine("The answer to {0} is  {1}", "I don't know", response["I don't know"]);
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900