get; set;

  }

  public string LastName {

   get; set;

  }

  public string Email {

   get; set;

  }

  public DateTime DOB {

   get; set;

  }

 }

}

Right-click the Contact class name and select Refactor→Extract Interface (see Figure 2-57).

Figure 2-57 

The Extract Interface dialog opens, and you can select the individual public members to form the interface (see Figure 2-58).

Figure 2-58 

The new interface is saved in a new .cs file. In this example, the filename is IContact.cs:

using System;

namespace WindowsFormsApplication1 {

 interface IContact {

  DateTime DOB { get; set; }

  string Email { get; set; }

  string FirstName { get; set; }

  string LastName { get; set; }

 }

}

The original Contact class definition has now been changed to implements the newly created interface:

class Contact : WindowsFormsApplication1.IContact {

 public string FirstName

 ...

Promote Local Variable to Parameter

You can promote a local variable into a parameter. Here's an example:

private void Form1_Load(object sender, EventArgs e) {

 LogError('File not found.');

}

private void LogError(string message) {

 string SourceFile = 'Form1.cs';

 Console.WriteLine(SourceFile + ': ' + message);

}

You want to promote the variable SourceFile into a parameter so that callers of this function can pass in its value through an argument. To do so, select the variable SourceFile, right- click, and then select Refactor→Promote Local Variable to Parameter (see Figure 2-59).

Figure 2-59

Note that the local variable to be promoted must be initialized or an error will occur. The promoted variable is now in the parameter list and the call to it is updated accordingly:

private void Form1_Load(object sender, EventArgs e) {

 LogError('File not found.', 'Form1.cs');

}

private void LogError(string message, string SourceFile) {

 Console.WriteLine(SourceFile + ': ' + message);

}

Debugging

Debugging is an important part of the development cycle. Naturally, Visual Studio 2008 contains debugging tools that enable you to observe the runtime behavior of your program. This section takes a look at those tools.

Suppose that you have the following program:

using System;

using System.Windows.Forms;

namespace WindowsFormsApplication1 {

 public partial class Form1 : Form {

  public Form1() {

   InitializeComponent();

  }

  private void Form1_Load(object sender, EventArgs e) {

   Console.WriteLine('Start');

   printAllOddNumbers(9);

   Console.WriteLine('End');

  }

  private void printAllOddNumbers(int num) {

   for (int i = 1; i <= num; i++) {

    if (i % 2 == 1) {

     Console.WriteLine(i);

    }

   }

  }

 }

}

The following sections show how you can insert breakpoints into the application so that you can debug the application during runtime.

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

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

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