Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Choices Constructor (String[])

Initializes a new instance of the Choices class from an array containing one or more String objects.

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

public Choices(
	params string[] phrases


Type: System.String[]

An array containing the set of alternatives.

Each String in phrases defines one alternative. The speech recognition engine can use any one of the items in the string array to match speech input. If phrases is an empty array, the constructor returns an empty set of alternatives. You can add alternatives using any of the Add methods.

The constructor throws an ArgumentNullException when phrases is null or any of the array elements are null. The constructor throws an ArgumentException if any element in the array is an empty string ("").

The following example uses Choices and GrammarBuilder objects to create a Grammar for the phrases such as, "Call Anne on her cell" and "Call James on his work phone". The example uses implicit casts from Choices and String to GrammarBuilder.

public Grammar CreatePhonePhrase()

  // Create alternatives for female names and add a phrase.
  GrammarBuilder females = new Choices(new string[] { "Anne", "Mary" });
  females.Append("on her");

  // Create alternatives for male names and add a phrase.
  GrammarBuilder males = new Choices(new string[] { "James", "Sam" });
  males.Append("on his");

  // Create a Choices object that contains an array of alternative
  // GrammarBuilder objects.
  Choices people = new Choices();
  people.Add(new Choices(new GrammarBuilder[] {females, males}));

  // Create a Choices object that contains a set of alternative phone types.
  Choices phoneType = new Choices();
  phoneType.Add(new string[] { "cell", "home", "work" });

  // Construct the phrase.
  GrammarBuilder gb = new GrammarBuilder();
  gb.Append(new GrammarBuilder("phone"), 0, 1);

  return new Grammar(gb);

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4
© 2015 Microsoft