GrammarBuilder Implicit Conversion (String^ to GrammarBuilder^)

 
equivalentCodeEntityM:System.Speech.Recognition.GrammarBuilder.#ctor(System.String)

Converts a string to a GrammarBuilder object.

Namespace:   System.Speech.Recognition
Assembly:  System.Speech (in System.Speech.dll)

public:
static operator GrammarBuilder^ (
	String^ phrase
)

Parameters

phrase
Type: System::String^

The string to convert.

Return Value

Type: System.Speech.Recognition::GrammarBuilder^

The converted string.

Implicit conversion creates a new instance of GrammarBuilder. This conversion operator is equivalent to calling GrammarBuilder and specifying the same phrase.

The following example uses GrammarBuilder and Choices objects to construct a grammar that can recognize either of the two phrases, "Make background colorChoice" or "Set background to colorChoice".

After creating a list of acceptable values for colorChoice using a Choices object, the example initializes two GrammarBuilder objects, makePhrase and setPhrase, using implicit conversion from string objects.

The example finally creates a Grammar object from a Choices object cast to a GrammarBuilder object.

private Grammar CreateColorGrammar()
{

  // Create a set of color choices.
  Choices colorChoice = new Choices(new string[] {"red", "green", "blue"});
  GrammarBuilder colorElement = new GrammarBuilder(colorChoice);

  // Create grammar builders for the two versions of the phrase.
  GrammarBuilder makePhrase = new GrammarBuilder("Make background");
  makePhrase.Append(colorElement);
  GrammarBuilder setPhrase = new GrammarBuilder("Set background to");
  setPhrase.Append(colorElement);

  // Create a Choices for the two alternative phrases, convert the Choices
  // to a GrammarBuilder, and construct the Grammar object from the result.
  Choices bothChoices = new Choices(new GrammarBuilder[] {makePhrase, setPhrase});
  Grammar grammar = new Grammar((GrammarBuilder)bothChoices);
  grammar.Name = "backgroundColor";
  return grammar;
}

.NET Framework
Available since 3.0
Return to top
Show: