Книга: C# 2008 Programmer

#warning and #error

#warning and #error

The #warning preprocessor directive lets you generate a warning from a specific location of your code. The following example shows how you can use it to display warning messages during compilation time.

for (int i = 1; i <= num; i++) {
 //---sum up all odd numbers---
 if (i % 2 == 1) {
  sum += i;
#if DEBUG
#warning Debugging mode is on
  Console.WriteLine("i={0}, sum={1}", i, sum);
#elif NORMAL
#warning Normal mode is on
  Console.WriteLine("sum={0}", sum);
#else
#warning Default mode is on
  Console.WriteLine(".");
#endif
}
}

Figure 3-16 shows the output when the DEBUG symbol is defined using the /define compiler option.


Figure 3-16

The #error preprocessor directive lets you generate an error. Consider the following example:

for (int i = 1; i <= num; i++) {
 //---sum up all odd numbers---
 if (i % 2 == 1) {
  sum += i;
#if DEBUG
#warning Debugging mode is on
  Console.WriteLine("i={0}, sum={1}", i, sum);
#elif NORMAL
#error This mode is obsolete.
  Console.WriteLine("sum={0}", sum);
#else
#warning Default mode is on
  Console.WriteLine(".");
#endif
 }
}

Here, if the NORMAL symbol is defined, an error message is shown and the statement defined within the conditional directive is ignored. Figure 3-17 shows that when you define the NORMAL symbol, the error message is displayed and the compilation is aborted.


Figure 3-17 

Оглавление книги


Генерация: 1.114. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз