Choices.Add Method (GrammarBuilder[])

Adds one of more GrammarBuilder object to the list of options already encapsulated in the current instance of Choices.

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




  • alternateChoices
    One or more GrammarBuilder objects, each of which represents a possible option for recognition.


Because of support for implicit conversion from Choices, SemanticResultKey, and SemanticResultValue to the GrammarBuilder, these three classes may be added to a Choices object as well.

Applications can freely intermix both of Add(String[]) and Add(GrammarBuilder[]).


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](

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.

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

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

    //Add grammar builder to choices.
    Choices choiceOfBuilders = new Choices();
    //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.


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


Choices Class
Choices Members
Microsoft.Speech.Recognition Namespace