Reputation: 185
I need help with a speech recognition program I'm working on in C#.
This refers the the Switch Statement. If we have an example this this:
//FIRST CASE STATEMENT
case "open chrome":
System.Diagnostics.Process.Start(@"C:\Program Files\Google\Chrome\Application\chrome.exe");
JARVIS.Speak("Loading");
break;
//SECOND CASE STATEMENT
case "Thanks":
JARVIS.Speak("No problem");
break;
How do I make it so that if the first case statement is not said then the second one will not work. But if the first case statement IS said then it will allow for the second one to work.
I'm thinking here I need an IF statement but I'm not sure.
Thanks.
Upvotes: 2
Views: 2108
Reputation: 48600
How about
//FIRST CASE STATEMENT
case "open chrome":
System.Diagnostics.Process.Start(@"C:\Program Files\Google\Chrome\Application\chrome.exe");
JARVIS.Speak("Loading");
alocalvariable = true;
break;
Outside switch
if (alocalvariable)
{
JARVIS.Speak("No problem");
alocalvariable = false;
}
Upvotes: 3
Reputation: 3615
You didn't really specify this, but the way this is set up, switch 1 will have to be hit one time for every time you want switch 2 to fire:
bool isValid = false;
switch(whateverYourVariableIsCalled)
{
//FIRST CASE STATEMENT
case "open chrome":
System.Diagnostics.Process.Start(@"C:\Program Files\Google\Chrome\Application\chrome.exe");
JARVIS.Speak("Loading");
isValid = true;
break;
//SECOND CASE STATEMENT
case "Thanks":
if (isValid)
{
JARVIS.Speak("No problem");
}
isValid = false;
break;
}
Upvotes: 2
Reputation: 2378
Switch/Case is like if-else statement, it will only execute one case.
You will need a nested if statement to do this.
To make your code more readable and understandable, you can probably make your cases into a function, and calls the function if satisfy certain condition.
Upvotes: 0