args array contains strings, so you need to explicitly convert the strings to integers with the Convert.ToInt32() method. Finally, the actual addition of the two strings is done at the end of the method, while they are being printed out. Note how C# is clever enough to have integer + integer be added together (in the case of num + num2), whereas string + integer attaches the integer to the end of the string (in the case of 'Sum of two parameters is:' + the result of num1 + num2). This isn't by accident: C# tries to convert data types cleverly, and warns you only if it can't convert a data type without losing some data. For example, if you try to treat a 64-bit integer as a 32-bit integer, it warns you because you might be throwing a lot of data away.

Adding Some Error Checking

Right now your program crashes in a nasty way if users don't provide at least two parameters. The reason for this is that we use arg[0] and arg[1] (the first and second parameters passed to your program) without even checking whether any parameters were passed in. This is easily solved: args is an array, and arrays can reveal their size. If the size doesn't match what you expect, you can bail out.

Add this code at the start of the Main() method:

if (args.Length != 2) {

 Console.WriteLine('You must provide exactly two parameters!');

 return;

}

The new piece of code in there is return, which is a C# keyword that forces it to exit the current method. As Main() is the only method being called, this has the effect of terminating the program because the user didn't supply two parameters.

Using the Length property of args, it is now possible for you to write your own Main() method that does different things, depending on how many parameters are provided. To do this properly, you need to use the else statement and nest multiple if statements like this:

if (args.Length == 2) {

 /// whatever...

} else if (args.Length == 3) {

 /// something else

} else if (args.Length == 4) {

 /// even more

} else {

 /// only executed if none of the others are

}

Building on Mono's Libraries

Fedora ships with several Mono-built programs, including Tomboy and Beagle. It also comes with a fair collection of .NET-enabled libraries, some of which you probably already installed earlier. The nice thing about Mono is that it lets you build on these libraries really easily: You just import them with a using statement, then get started.

To demonstrate how easy it is to build more complicated Mono applications, we're going to produce two: one using Beagle, the super-fast file indexer, and one using Gtk#, the GUI toolkit that's fast becoming the standard for Gnome development. Each has its own API that takes some time to master fully, but you can get started with them in minutes.

Searching with Beagle

Beagle is the de facto Linux search tool for Gnome, and is also used by several KDE-based programs. It works by scanning your computer in the background, then monitoring for file system changes so that its data always stays up to date. However, the magic is that it indexes data cleverly — if you tag your images, it reads those tags. If you have album and artist data in your MP3s, it reads that data too. It also reads your emails, your instant messenger conversations, your web browser history, and much more—and provides all this data in one place, so if you search for 'firefox' you'll find the application itself, all the times you've mentioned Firefox in your emails, and so on.

In MonoDevelop, go to File, New Project, select C#, then choose Console Project. Give it the name BeagleTest, and tell MonoDevelop not to create a separate directory for the solution. You'll be back at the default Hello World program, but you're going to change that. First, you need to tell Mono that you want to use Beagle and Gtk#. No, you're not going to create a GUI for your search, but you do want to take advantage of Gtk#'s idle loop system — we'll explain why soon.

To add references to these two libraries, right-click on the word References in the left pane (just above Resources) and select Edit References. A new window appears (shown in Figure 29.3), and from that you should make sure Beagle and gtk-sharp are selected. Now click OK, and the References group on the left should expand so that you can see you have Beagle, gtk-sharp, and System (the last one is the default reference for .NET programs).

FIGURE 29.3 You need to tell Mono exactly which resource libraries you want to import for your program.

Now it's time to write the code. At the top of the Main.cs file (your main code file), you need to edit the 'using' statements to look like this:

using System;

using System.Collections;

using Beagle;

using Gtk;

The BeagleTest namespace and MainClass class aren't changing, but you do need to edit the Main() method so that you can run your Beagle query. Here's how it should look, with C# comments (//, as in C++) sprinkled throughout to help you understand what's going on:

public static void Main(string[] args) {

 Application.Init();

 // 'Query' is the main Beagle search type.

 //It does lots of magic for you - you just need to provide it with a search term and tell it where to search

 Query q = new Query();

 // these two are callback functions.

 // What you're saying is, when a hit is returned

Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату