SpObjectToken Example
The following code example demonstrates the use of the Count property and the Item method. The sample displays all available recognizers on the computer. The available audio input devices (sound cards) or profiles may also be displayed by remarking out the other two lines and removing the remarks from the desired line.
The example works for all objects returned as ISpeechObjectTokens. ISpeechObjectTokens is actually a collection of objects. The Count property returns the number of items in the collection. The Item method returns an individual member at the given index.
The type of the members in the collection will be different based on the creating call. In this example code, the collection is a list of recognizers.
To run this code, create a form with the following control:
Paste this code into the Declarations section of the form.
Private Sub Form_Load()
Dim SharedRecognizer As SpSharedRecognizer
Set SharedRecognizer = New SpSharedRecognizer
Dim theResources As ISpeechObjectTokens
Set theResources = SharedRecognizer.GetRecognizers
'Set theResources = SharedRecognizer.GetAudioInputs
'Set theResources = SharedRecognizer.GetProfiles
Dim i As Long
Dim recoObject As SpObjectToken
Label1.Caption = ""
For i = 0 To theResources.Count - 1
Set recoObject = theResources.Item(i)
Label1.Caption = Label1.Caption & recoObject.GetDescription & vbCrLf
Next i
End Sub
Count and Item may also be used for other object collections. The following code snippet demonstrates the use of the Count property and the Item method for ISpeechLexiconWords and ISpeechLexiconWord. The collection itself is of type ISpeechLexiconWords and the individual members of are type ISpeechLexiconWord. Other collections may use Count and Item in the same manner. For instance ISpeechGrammarRules and ISpeechGrammarRule may be substituted respectively for the collection and member type.
For the sake of brevity, CreateCollection is assumed to be a function that creates the collection, again for this example of ISpeechLexiconWords. The Count property returns the count of member items and the Item method returns a specific member of the collection.Dim C As ISpeechLexiconWords 'The collection
Dim M As ISpeechLexiconWord 'An item in the collection
Set C = CreateCollection 'Create the collection
'Get last member of the collection in object "M"
lngCount = C.Count 'How many items
If lngCount Then
Set M = C.Item(lngCount - 1) 'Get the last one
End If