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
Expand Minimize

ObjectsAndClasses Overview (SAPI 5.4)

Speech API 5.4
Microsoft Speech API 5.4

Objects and Classes Overview

Classes

For automation to work, there must be technology common to all applications. Though still evolving, the current incarnation of that technology is the component object model (COM). Many applications are COM-compliant and the usage is widespread. Using this technology, COM-compliant applications can issue and receive commands from other applications. The details of COM and automation are complex, but almost all of the issues are hidden from the user or application developers.

The functionality of these components is encapsulated in distinct files called type libraries. Type libraries contain all the methods, properties, and supporting data for the applications to use; the application just needs to load the appropriate type library to gain access to the functionality. In Visual Basic, type libraries are also called references. Some type libraries are loaded automatically and others must be loaded explicitly. This will be covered later and in more detail. Once loaded, Visual Basic has complete access to the contents of the type library.

Libraries themselves contain many separate items. For example, The Microsoft Speech Object Library (once loaded, it is called SpeechLib in the references) contains over 400 individual methods and properties, not including supporting data. Therefore, to better organize functionality, related calls and data are grouped together in classes. For example, SAPI has an ISpeechRecoResult class. This class contains functionality needed to assess the results of successful speech recognition. To retrieve the text of the spoken recognition, use the GetText method from ISpeechRecoResult.

However, classes themselves are just a blueprint for the functionality. That is, classes describe the functions, but they cannot execute the function. To execute the function, an object must be created (also called instantiated). In this way, one class may be instantiated any number of times.

Once an object is created, Visual Basic uses a standard notation for making these calls. This is an object.method notation (pronounced "object dot method"). A typical call will look like this: ISpeechRecoResult.PhraseInfo.GetText(). Complete ownership could also be used by including the library name: SpeechLib.ISpeechRecoResult.PhraseInfo.GetText().

To determine what is available from a particular type library after loading it, Visual Basic offers an object browser. The browser is available by either pressing F2 or selecting View->Object Browser from the menu. The object browser displays the information in a graphical format. Clicking or double-clicking an item reveals additional information about it including properties, methods, events, or classes that it contains. The bottom pane displays a quick reference guide for the selection and includes the owning object memberships up through the library. This method of browsing available objects can be useful when learning a new library. In addition, two additional modules SpeechConstants and SpeechStringConstants, are available for viewing. These two modules contain names for numeric and string constants used by SAPI.

Objects

As mentioned, classes are just blueprints for a certain functionality. To use the calls of a class, an object must be created. An object can allocate memory and assign values to various elements contained by the object. Making an instance of the object is similar to declaring variables. For example, the variable type String is a common occurrence in Visual Basic programs. The type allows a character string to be used, saving information in it, and later retrieving information from it. However, before using the variable an instance must be created. In the following example, one String instance is created:


Dim myString As String

After this appears in the code, the variable myString is valid and may be referenced and changed as needed. In this sense, String can be considered the class and myString is an instance of the class. Likewise, it is possible to declare several instances of String.


Dim myString, userName, fileName As String

Now three strings are available for use, presumably to record information for vastly different purposes. Perhaps userName may be used to track the user's identity, and fileName for recording the location and name of a file. This is an example of one data type having several instances. The same will hold true for classes and interfaces.

Instantiating an object is similar, but requires slight modifications. The additional modifications are in the second line:

   Dim myVoice As SpVoice
   Set myVoice = New SpVoice

The additional keywords are Set and New. Classes require these two keywords since classes are more complex than variables. Once declared and allocated, the object may be used and referenced. However, simply creating an object may not be enough and additional initialization may be required. Just as creating myString allocates the variable for use, the programmer must assign a value to it. In fact, it is possible for an object to have no valid reference. In this case, the value will be Nothing. The following code tests for an object which is Nothing.

If myVoice Is Nothing Then
   'Handle the situation here. You might want to allocate it or
   'warn the user an error could have occurred.
End If
Show:
© 2015 Microsoft