Choices.ToGrammarBuilder Method

Returns an instance of GrammarBuilder constructed from the current Choices instance.

Namespace: Microsoft.Speech.Recognition
Assembly: Microsoft.Speech (in microsoft.speech.dll)

Syntax

'Declaration




Return Value

An instance of GrammarBuilder created from the current instance of Choices.

Remarks

The use of ToGrammarBuilder is equivalent to using the GrammarBuilder constructor which takes Choices as an argument (GrammarBuilder(Choices)).

Example

In the example below Choices objects are created, than then added to so as to create a grammar supporting a choice of two command "Set background to [choice color string]" and "Change background to [choice color string]".

The choice of color strings is created using the Add(String[]) method, and used to create to GrammarBuilder objects.

The GrammarBuilder objects are then combined to into a Choices using [Add](https://msdn.microsoft.com/en-us/library/m:microsoft.speech.recognition.choices.add(microsoft.speech.recognition.grammarbuilder%5b%5d(v=office.13)).

A final GrammarBuilder instance is created, created using the ToGrammarBuilder method on Choices, and the appropriate grammar returned.

private Grammar CreateWithChoiceAdd() {
    Choices colorChoice = new Choices();      // Associate the string name of the color with each item.

    foreach (string colorName in System.Enum.GetNames(typeof(KnownColor))) {
        // Associate the string name of the color with each item.
        colorChoice.Add(colorName);
    }

    // Syntax "Set Background to [color]"
    GrammarBuilder setBackgroundToBuilder = new GrammarBuilder("Set Background to");
    setBackgroundToBuilder.Append(colorChoice);

    // Syntax "change Background to [color]"
    GrammarBuilder changeBackgroundToBuilder = new GrammarBuilder("change Background to");
    changeBackgroundToBuilder.Append(colorChoice);

    //Add grammar builder to choices.
    Choices choiceOfBuilders = new Choices();
    choiceOfBuilders.Add(setBackgroundToBuilder,changeBackgroundToBuilder);
    //NB We could have done with a constructor.

    GrammarBuilder gb = choiceOfBuilders.ToGrammarBuilder();
    Grammar grammar = new Grammar(gb);
    grammar.Name = "Set/Change Background to";

    return grammar;
}

Thread Safety

All public static (Shared in Visual Basic) members of this type are thread-safe. Instance members are not guaranteed to be thread-safe.

Platforms

Development Platforms

Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Vista Ultimate Edition, Windows Vista Business Edition, Windows Vista Enterprise Edition

Target Platforms

See Also

Reference

Choices Class
Choices Members
Microsoft.Speech.Recognition Namespace
GrammarBuilder
Grammar