AddThis Social Bookmark Button

Print

Refactoring in Whidbey
Pages: 1, 2, 3, 4

You have the options to update either all external references or all references (including the one within the class). You can also preview the changes before they are made (see Figure 5).


Figure 5. Previewing the changes

Here is the result after applying the change to external references:

public class MyPoint
{
  public float x, y;
  public float X
  {
    get
    {
       return x;
    }
  
    set
    {
       x = value;
    }
  }
  public void MyPoint() {}
  public MyPoint(float _x, float _y) {
    x = _x;   // assigns through the member variable
    y = _y;
  }
}

MyPoint ptA = new MyPoint();
ptA.X = 5;   
ptA.y = 6;

And here is the result if you were to apply to all references:

public class MyPoint {
  public float x, y;
  public float X
  {
    get {
      return x;
    }
  
    set {
       x = value;
    }
  }
  public void MyPoint() {}
  public MyPoint(float _x, float _y) {
    X = _x;
    y = _y;
  }
}

MyPoint ptA = new MyPoint();
ptA.X = 5;   // assigns through the property
ptA.y = 6;

The difference lies in the assignment of the variable within the MyPoint() constructor (note the capitalization of X in either case). In general, it is always advisable to apply the update to all references. Also note that after the refactoring, you need to change the access modifier of x to private.

Extract Interface

You can also use the refactoring engine to extract an interface from a class definition. Consider the following example:

public class MyPoint {
  
  private float x, y;
  
  public float Y
  {
    get {
      return y;
    }
  
    set {
      y = value;
    }
  }
  
  public float X {
    get {
       return x;
    }
  
    set
    {
       x = value;
    }
  }
  
  public void MyPoint() {}

  public MyPoint(float _x, float _y) {
    X = _x;
    Y = _y;
  }

  public float getDistanceFromO() {
    // code implementations here
  }

  public float getDistanceFromPoint() {
    // code implementations here
  }
}

Right-click on any line within the class and select Refactor -> Extract Interface... (see Figure 6)


Figure 6. Extracting an interface from a class definition

The Extract Interface dialog will be shown. You can select the individual public members to form the interface (see Figure 7).


Figure 7. The Extract Interface dialog

The new interface will now be saved in a new .cs file:

using System;
namespace WindowsApplication1 {
  interface IMyPoint   {
    float getDistanceFromO();
    float getDistanceFromPoint();
    void MyPoint(float _x, float _y);
    float X { get; set; }
    float Y { get; set; }
  }
}

The original class definition now implements the newly created interface:

public class MyPoint : WindowsApplication1.IMyPoint
{    
  ...
  ...

Pages: 1, 2, 3, 4

Next Pagearrow