15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

402 ❘ ChaPTer 16 visuAl studiO 2010<br />

Now, suppose that in the idea of refactoring, you want to change the code a bit so that the color <strong>and</strong> the<br />

door variables are encapsulated in public .<strong>NET</strong> properties. The refactoring capabilities of Visual Studio<br />

2010 allow you to simply right-click either of these properties in the document window <strong>and</strong> select Refactor<br />

➪ Encapsulate Field. This will pull up the Encapsulate Field dialog shown in Figure 16-28.<br />

figure 16-28<br />

From this dialog, you can provide the name of the property <strong>and</strong> click the OK button. This will turn the<br />

selected public field into a private field, while also encapsulating the field in a public .<strong>NET</strong> property. After<br />

you click OK, the code will be reworked into the following (after redoing both fields):<br />

namespace ConsoleApplication1<br />

{<br />

public class Car<br />

{<br />

private string _color;<br />

public string Color<br />

{<br />

get { return _color; }<br />

set { _color = value; }<br />

}<br />

private string _doors;<br />

public string Doors<br />

{<br />

get { return _doors; }<br />

set { _doors = value; }<br />

}<br />

}<br />

}<br />

public int Go()<br />

{<br />

int speedMph = 100;<br />

return speedMph;<br />

}<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!