Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать
object ignoreCase = true; object alwaysSuggest = false;

CHAPTER 25 OTHER TOPICS

Interoperating with COM

Although this text doesn’t cover COM programming, C# 4.0 added several syntactic changes to the language specifically to make COM programming easier. One of these changes is called the “omit ref” feature and allows you to call a COM method without using the ref keyword, when you don’t need to use the value passed back by the method.

For example, if Microsoft Word is installed on the machine your program is running on, you can use Word’s spell checker functionality in your own program. The method you would use to do this is the

CheckSpelling method on the Document class, which is in the Microsoft.Office.Tools.Word namespace. This method has 12 parameters, and all of them are ref parameters. This means previously you would have had to supply reference variables for each of the parameters, even if you didn’t need to use them to pass data to the method or to receive data back from the method. Omitting the ref keyword only works with COM methods—with anything else, you’ll still get a compile error.

This code might look something like the following. Notice the following about this code:

I’m only using the second and third parameters, which are Booleans, but I have to create two variables, ignoreCase and alwaysSuggest of type object to hold the values, since the method requires ref parameters.

I’ve created an object variable called optional for the other ten parameters.

Objects to hold Boolean variables object optional = Missing.Value; ↓ ↓ tempDoc.CheckSpelling( ref optional, ref ignoreCase, ref alwaysSuggest,

ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional );

With the “omit ref” feature we can clean this up considerably, since we don’t have to use the ref keyword on those parameters from which we don’t need the output, and we can use inline bools for the two parameters we care about. The simplified code looks like the following:

 

 

 

bool

bool

object optional = Missing.Value;

tempDoc.CheckSpelling( optional,

true, false,

optional,

optional,

optional, optional,

optional,

optional,

optional, optional, optional );

If, beyond the “omit ref” feature, we add the fact that the parameters are optional, we can use the option parameters feature of C# 4.0. This looks like the following, which is much less cumbersome than the original:

tempDoc.CheckSpelling( Missing.Value, true, false );

689

CHAPTER 25 OTHER TOPICS

The following code includes this method in a complete program. To compile this code, you need to have Visual Studio Tools for Office installed on your machine, and you must add a reference in your project to the Microsoft.Office.Interop.Word assembly. For the compiled code to run, you must have Microsoft Word installed on your machine.

using System;

using System.Reflection;

using Microsoft.Office.Interop.Word;

class Program

{

static void Main()

{

Console.WriteLine( "Enter a string to spell-check:" ); string stringToSpellCheck = Console.ReadLine();

string spellingResults; int errors = 0;

if ( stringToSpellCheck.Length == 0 ) spellingResults = "No string to check";

else

{

Microsoft.Office.Interop.Word.Application app =

new Microsoft.Office.Interop.Word.Application();

Console.WriteLine( "\nChecking the string for misspellings ..." ); app.Visible = false;

Microsoft.Office.Interop.Word._Document tempDoc = app.Documents.Add( );

tempDoc.Words.First.InsertBefore( stringToSpellCheck ); Microsoft.Office.Interop.Word.ProofreadingErrors

spellErrorsColl = tempDoc.SpellingErrors; errors = spellErrorsColl.Count;

//1. Before C# 4.0

 

 

//object ignoreCase

=

true;

//object

alwaysSuggest

=

false;

//object

optional

=

Missing.Value;

//tempDoc.CheckSpelling(

ref optional, ref ignoreCase, ref alwaysSuggest,

//ref optional, ref optional, ref optional, ref optional, ref optional,

//ref optional, ref optional, ref optional, ref optional );

690

CHAPTER 25 OTHER TOPICS

//2. Using the "omit ref" feature of C# 4.0 object optional = Missing.Value; tempDoc.CheckSpelling(

optional, true, false, optional, optional, optional, optional, optional, optional, optional, optional, optional );

//3. Using "omit ref" and optional parameters //tempDoc.CheckSpelling( Missing.Value, true, false );

app.Quit(false);

spellingResults = errors + " errors found";

}

Console.WriteLine( spellingResults );

Console.WriteLine( "\nPress <Enter> to exit program." ); Console.ReadLine();

}

}

691

CHAPTER 25 OTHER TOPICS

When you run this code, it produces a console window, shown in Figure 25-9, that asks you to enter a string that you want run through the spell checker. When it receives the string, it opens Word and runs the spell checker on it. When that happens, you’ll see Word’s spell checker window appear, as shown in Figure 25-10.

Figure 25-9. The console window that asks for the string to send to Word’s spell checker

Figure 25-10. Word’s spell checker created using COM calls from the console program

692

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]