ShouldBeInInterface method to be extracted from the Extract Interface dialog introduces a new
interface (in a new file) and updates the original class as follows:
c#
interface IBestPractice
{
void ShouldBeInInterface();
}
public class ConcreteClass: Chapter08Sample.IBestPractice
{
public void ShouldBeInInterface()
{ /* ... */ }
public void NormalMethod(int ParameterA, int ParameterB)
{ /* ... */ }
public void AnotherNormalMethod()
{ /* ... */ }
}
Extracting an interface is also available within CodeRush Xpress, however it doesn’t allow you
to choose which methods you want to include in the interface. Unlike the C# interface extraction,
which places the interface in a separate file (which is recommended), CodeRush Xpress simply
extracts all public class methods into an interface in the same code file. For example, using
CodeRush Xpress’s Extract Interface refactoring action on the following class:
Vb
Public Class ConcreteClass
Public Sub ShouldBeInInterface()
'...
End Sub
Public Sub NormalMethod(ByVal ParameterA As Integer,
ByVal ParameterB As Integer)
'...
End Sub
Public Sub AnotherNormalMethod()
'...
End Sub
End Class
refactoring actions .
153
Will result in the following code:
Vb
Public Interface IConcreteClass
Sub ShouldBeInInterface()
Sub NormalMethod(ByVal ParameterA As Integer, ByVal ParameterB As Integer)
Sub AnotherNormalMethod()
End Interface
Public Class ConcreteClass
Implements IConcreteClass
Public Sub ShouldBeInInterface() Implements IConcreteClass.ShouldBeInInterface
'...
End Sub
Public Sub NormalMethod(ByVal ParameterA As Integer,
ByVal ParameterB As Integer) _
Implements IConcreteClass.NormalMethod
'...
End Sub
Public Sub AnotherNormalMethod() Implements IConcreteClass.AnotherNormalMethod
'...
End Sub
End Class
reorder Parameters
Sometimes it’s necessary to reorder
parameters. This is often for cosmetic reasons,
but it can also aid readability and is sometimes
warranted when implementing interfaces.
The Reorder Parameters dialog, shown in
Figure 8-18, enables you to move parameters
up and down in the list according to the order
in which you want them to appear.
Once you establish the correct order, you’re
given the opportunity to preview the changes.
By default, the parameters in every reference
to this method are reordered according to the
new order. The Preview dialog, similar to
the one shown in Figure 8-15, enables you to
control which references are updated.
The CodeRush Xpress experience for
reordering parameters is somewhat more
intuitive than the native action for C#. Again,
the creators have opted for visual aids instead
of a modal dialog, as shown in Figure 8-19.
You can move the selected parameter left or
fiGure 8-18
fiGure 8-19
154 .
chaPter 8 code SnippeTS And reFAcToring
right in the parameter list and navigate between parameters with the Tab key. Once the parameters
are in the desired order, the search and replace interface, illustrated in Figure 8-16, enables the
developer to verify all updates.
remove Parameters
When removing a parameter from a method, using the refactoring function to do this considerably
reduces the amount of searching that has to be done for compile errors that can occur when a
parameter is removed. The other time this action is particularly useful is when there are multiple
overloads for a method, and removing a parameter may not generate compile errors; in such a case,
runtime errors may occur due to semantic, rather than syntactical, mistakes.
Figure 8-20 illustrates the Remove Parameters dialog that is used to remove parameters from the
parameters list. If a parameter is accidentally removed, it can be easily restored until the correct
parameter list is arranged. As the warning on this dialog indicates, removing parameters can often
result in unexpected functional errors, so it is important to review the changes made. Again, you
can use the preview window to validate the proposed changes.
CodeRush Xpress only supports removing unused parameters, as shown in Figure 8-21.
fiGure 8-20 fiGure 8-21
rename
Visual Studio 2010 provides rename support
in both C# and VB. The Rename dialog for
C# is shown in Figure 8-22; it is similar in
VB although it doesn’t have the options to
search in comments or strings.
Unlike the C# rename support, which
displays the preview window so you can
confirm your changes, the rename capability
in VB simply renames all references to that
fiGure 8-22
variable.
refactoring actions .
155
Promote Variable to Parameter
One of the most common refactoring techniques is to adapt an existing method to accept an
additional parameter. Promoting a method variable to a parameter makes the method more generic.
It also promotes code reuse. Intuitively, this operation would introduce compile errors wherever the
method was referenced. However, the catch is that the variable you are promoting to a parameter
must have an initial constant value. This value is added as a parameter value to all the method
references to prevent any changes to functionality. Starting with the following snippet, if the method
variable output is promoted, you end up with the second snippet:
Vb
Private Sub MethodA()
MethodB()
End Sub
Private Sub MethodB()
Dim output As String = "Test String"
MessageBox.Show(output)
End Sub
c#
public void MethodA()
{
MethodB();
}
public void MethodB()
{
string output = "Test String";
MessageBox.Show( output);
}
After the variable is promoted, you can see that the initial value is now being passed through as a
parameter wherever this method is referenced:
Vb
Private Sub MethodA()
MethodB("Test String")
End Sub
Private Sub MethodB(ByVal output As String)
MessageBox.Show(output)
End Sub
c#
public void MethodA()
{
MethodB("Test String");
}
public void MethodB(string output)
{
MessageBox.Show( output);
}
156 .
chaPter 8 code SnippeTS And reFAcToring
Generate Method stub
As you write code, you may realize that you need to call a method that you haven’t written yet. For
example, the following snippet illustrates a new method that you need to generate at some later stage:
Vb
Private Sub MethodA()
Dim InputA As String
Dim InputB As Double
Dim OutputC As Integer = NewMethodIJustThoughtOf(InputA, InputB)
End Sub
c#
public void MethodA()
{
string InputA;
double InputB;
int OutputC = NewMethodIJustThoughtOf(InputA, InputB);
}
Of course, the preceding code generates a build error because this method has not been defined.
Using the Generate Method Stub refactoring action (available as a smart tag in the code itself), you
can generate a method stub. As you can see from the following sample, the method stub is complete
with input parameters and output type:
Vb
Private Function NewMethodIJustThoughtOf(ByVal InputA As String,
ByVal InputB As Double) As Integer
Throw New NotImplementedException
End Function
c#
private int NewMethodIJustThoughtOf(string InputA, double InputB)
{
throw new Exception("The method or operation is not implemented.");
}
organize usings
It’s good practice to maintain a sorted list of Using statements in each file (in C#), and only reference
those namespaces that are actually required within that file. The Organize Usings menu (available
from the context menu when right-clicking in the code editor as shown in Figure 8-23) can help you
in both these cases.
fiGure 8-23
After a major refactoring of your code you may fi nd that you have a number of using directives at the
top of your code fi le that are no longer being used. Rather than going through a process of trial and error
to determine what is and isn ’ t being used, you can use an operation in Visual Studio to do this for you
by right - clicking in the code editor and choosing Organize Usings . Remove Unused Usings (C# only).
Using directives, using aliases, and external assembly aliases not being used in the code fi le are removed.
VB developers don ’ t have a way to sort and remove unused Imports statements.
However, on the References tab on the Project Properties dialog, it ’ s possible to
mark namespaces to be imported into every code fi le. This can save signifi cantly
on the number of Imports statements. On this page you also have the ability to
remove unused assembly references.
It ’ s good practice to organize the using directives in alphabetical order to make it easy to manage
what namespaces are being referenced. To save you doing this manually you can right - click in
the code editor and choose Organize Usings . Sort Usings to have Visual Studio do this for you.
The using directives from the System namespace appear fi rst, then the using directives from other
namespaces appear in alphabetical order. If you have aliases defi ned for namespaces, these are
moved to the bottom of the list, and if you are using external assembly aliases (using the extern
keyword in C#), these are moved to the top of the list.
To sort using directives and remove those that are not being used in one action, right - click in the
code editor and choose Organize Usings . Remove and Sort.
The default Visual Studio template code fi les have the using statements at the top of
the fi le outside the namespace block. However, if you are following the StyleCop
guidelines these specify that using statements should be contained within the
namespace block. The Organize Usings functions handle either situation based upon
the current location of the using statements in the fi le and retaining that location.
suMMary
Code snippets are a valuable inclusion in the Visual Studio 2010 feature set. You learned in this
chapter how to use them, and how to create your own, including variable substitution (and Imports
and reference associations for VB snippets). With this information you ’ ll be able to create your
own library of code snippets from functionality that you use frequently, saving you time in coding
similar constructs later.
This chapter also provided examples of each of the refactoring actions available within Visual Studio
2010. Although VB developers do not get complete refactoring support out of the box, CodeRush
Xpress provides a wide range of refactoring actions that enable them to easily refactor their projects.
summary . 157
9 9
server explorer
what’s in this chaPter?
.
Querying hardware resources and services on local and remote
computers
.
Using the Server Explorer to easily add code that
works with computer resources to your applications
The Server Explorer is one of the few tool windows in Visual
Studio that is not specific to a solution or project. It allows
you to explore and query hardware resources and services on
local or remote computers. You can perform various tasks and
activities with these resources, including adding them to your
applications.
The Server Explorer, shown in Figure 9 - 1, has three types of
resources to which it can connect. The first, under the Servers
node, enables you to access hardware resources and services on a
local or remote computer. This functionality is explored in detail
in this chapter. The second type of resources is under the Data
Connections node and allows you to work with all aspects of data connections, including the
ability to create databases, add and modify tables, build relationships, and even execute queries.
Chapter 26 covers the Data Connections functionality in detail. Finally, you can add a connection
to a SharePoint server and browse SharePoint - specific resources such as Content Types, Lists,
Libraries, and Workflows. SharePoint connections are covered in more detail in Chapter 24.
serVer connections
The Servers node would be better named Computers, because it can be used to attach to and
interrogate any computer to which you have access, regardless of whether it is a server or a
desktop workstation. Each computer is listed as a separate node under the Servers node. Below
fiGure 9-1
160 .
chaPter 9 SerVer explorer
each computer node is a list of the hardware, services, and other components that belong to that
computer. Each of these contains a number of activities or tasks that can be performed. Several software
vendors have components that plug into and extend the functionality provided by the Server Explorer.
To access Server Explorer, select Server Explorer on the View menu. By default, the local computer
appears in the Servers list. To add another computer, right-click the Servers node and select Add
Server from the context menu.
Entering a computer name or IP address initiates
an attempt to connect to the machine using your
credentials. If you do not have suffi cient privileges,
you can elect to connect using a different username
by clicking the appropriate link. The link appears to
be disabled, but clicking it does bring up a dialog,
shown in Figure 9 - 2, in which you can provide an
alternative username and password.
fiGure 9-2
You will need Administrator privileges on any server that you want to access
through the Server Explorer.
event logs
The Event Logs node gives you access to the machine
event logs. You can launch the Event Viewer from the
right-click context menu. Alternatively, as shown in
Figure 9-3, you can drill into the list of event logs to
view the events for a particular application. Clicking
any of the events displays information about the event
in the Properties window.
Although the Server Explorer is useful for