So I'm at a client's site and we're discussing adding a new method to an existing class. One of the client's programmers said, "We should put the method in the base class and let all the derived classes inherit it from it" Another programmer said, "I think it would be better to create a new class that inherits from our base class and add the new method to it."
And I said, "Let's not change anything. Let's just create the new method."
More
Posted by Peter Vogel on 12/03/20122 comments
It's hard to create memory leaks in .NET, but I can do it when developing in SharePoint without even trying very hard. The SharePoint Dispose Check (SPDisposeCheck to its friends) analyzes your code to see if you're following Microsoft's best practices in disposing of SharePoint resources.
SPDisposeCheck is a Visual Studio Add-In that adds a new menu item to your Tools menu. When you select it, SPDisposeCheck pops up a dialog that lets you check which parts of your Solution will be checked. In addition to running an analysis when you want, you can also choose to have your code analyzed as part of building your projects. By default, the results are displayed in the Output Window (you have to set the dropdown list at the top of the Window to SPDispose check). However, you can also choose to have any problems listed as Errors.
More
Posted by Peter Vogel on 11/29/20120 comments
Scattered across all the services I've created, I must have 257 methods whose names begin with the word "Get" (e.g., GetCustomerData, GetOrdersForDate). And I typically have a corresponding method for accepting the data (e.g., UpdateCustomerData, SetOrders). I wouldn't write a class like that with two separate methods; I'd create a Property just called, for instance, CustomerData.
It turns out that you can use properties in a WCF Service: just put the OperationContract attributes on the property's getter and setter:
More
Posted by Peter Vogel on 11/26/20120 comments
I'm not sure whether to classify this as a tip or bad advice: It's possible to redefine the operators (e.g. !, =, ++) used with a class. That doesn't mean it's a good idea. As I discussed in an earlier tip, the differences in using equality tests between C# and VB (and, within a language, the differences between the equals operator and the Equals method) probably create enough confusion without you assigning an arbitrary meaning to =/== for a particular class. But having said that…
More
Posted by Peter Vogel on 10/16/20121 comments
Entity Framework gets all the attention, but there's still something to like about ADO.NET DataSets. For instance, one reason that typed DataSets remain popular is that you can dramatically simplify your code by extending the DataSet with your own custom TableAdapter methods. All you have to be able to do is create an SQL statement in the Visual Studio query designer: Visual Studio will generate the code for you.
Ordinarily, to fill a table in a DataSet you need a half-dozen lines to open a connection, create an adapter (with its commands and parameters), set values in the adapter's parameters, call the adapter's Fill method, and then retrieve the resulting table (without, of course, forgetting to close the connection).
More
Posted by Peter Vogel on 10/09/20120 comments
It's not unusual to include a link in your page with a URL that points to a PDF file to let your users view the file on their computer. However, you can also control, through the URL, where and how the PDF file is displayed: just add a hash/pound sign (#) and some parameters to the PDF's URL.
For instance, the page parameter lets you open the PDF file at a specific page. This example opens the file at page 3:
http://mysite.com/userguide.pdf#page=3
More
Posted by Peter Vogel on 10/03/20120 comments
A reader responded to an earlier tip on Visual Studio keyboard shortcuts by recommending VsVim, by Jared Parker. If the idea of never taking your fingers off the home row of your keyboard again, never having to use a menu, and never, ever reaching for the mouse sounds like heaven to you, then Jared's right: you should be looking at VsVim.
If using the letter d to delete, using uppercase D to delete to end of line, and using dd to delete a whole line is something you'll remember, then you're probably a veteran Unix programmer. In that case, you'll find the Visual Studio focus on WIMP (Windows, Icons, Menus, Pointer), well, wimpy. VsVim will put you back in control.
More
Posted by Peter Vogel on 09/25/20120 comments
Do you get tired of scrolling through the New Project Item lists to get to the item templates that you need, while skipping the ones that you'll never use? The same is probably true, though to a lesser extent, with the New Project dialog.
Why not slim those lists down to the half-dozen items you actually use that will, as a result, be right there when you need them? This probably goes against the grain of most developers' attitude towards new technology, but let's face it: you (and your organization) have probably figured out which templates you are and aren't going to use in Visual Studio.
More
Posted by Peter Vogel on 08/23/20122 comments
I was recently asked by a WPF programmer how to modify a value passed between bound properties in a XAML file. You can't, for instance, integrate math into a binding expression, as this example does, to divide the value returned by 2:
<Image Height="100" Width="100" Name="MyImage" Source="Me.jpg"
Opacity="{Binding ElementName=MySlider, Path=Value/2}" /&rt;
This doesn't work, either:
<Image Height="225" Width="300" Name="CurrentImage" Source="Me.jpg"
Opacity="{Binding ElementName=MySlider, Path=Value}/2" /&rt;
While XAML gives you a lot of options when binding a property on one control to something else, modifying the data isn't allowed; at least, not directly. The answer is to use a custom converter. Programmers usually create converters to handle converting the data type of one property to the data type to another property (though XAML will handle a lot of conversions -- numerics to strings, for instance --on its own). But you can also use converters to massage the data in any way you want. This example, for instance, binds the Opacity setting for an Image control to the value in a slider control:
<Slider Margin="10" Name="MySlider" Minimum="0.0"
Maximum="2" Value="0.5"/&rt;
<Image Height="100" Width="100" Name="MyImage" Source="Me.jpg"
Opacity="{Binding ElementName=MySlider, Path=Value}" /&rt;
Let's say that you want the transparency to be one-half of the value in the Slider: a converter will solve the problem. This converter, for instance, divides the number passed to it by 2 in its Convert method:
[ValueConversion(typeof(int), typeof(int))]
public class Divider : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
return object/2;
}
public object ConvertBack(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
return object * 2;
}
}
You need to do three things to use your converter. First, define a namespace for your converter (I've assumed that the converter is in the same project as the XAML file and the namespace for the project is MyApp):
<Window x:Class="MyClass"
…
xmlns:current="clr-namespace:MyApp"&rt;
Then you have to add your converter to your resources. I'm adding this converter to the Window's resources with the key MyDivider:
<Window.Resources&rt;
<current:Divider x:Key="MyDivider"/&rt;
</Window.Resources&rt;
Finally, invoke your converter by passing the resource's key to the StaticResource object's constructor to retrieve it from the resources collection, and then passing that result to the Binding object's Converter property, like this:
<Image Height="100" Width="100" Name="MyImage" Source="Me.jpg"
Opacity="{Binding ElementName=MySlider, Path=Value
Converter={StaticResource MyDivider}}
Posted by Peter Vogel on 08/09/20123 comments
Probably the most popular tip has been in this column has been the one on four Visual Studio keyboard shortcuts. But if you want, you can create your own shortcuts for any Visual Studio commands you use a lot. Of course, most commands are already assigned to keyboard shortcuts, but this feature also lets you define a command to some key combination you'll actually remember.
More
Posted by Peter Vogel on 07/29/20122 comments
In an earlier column I discussed using the Register and Resolve methods with Microsoft's Unity dependency container. However, if you've got all of Visual Studio's hotfixes applied, you'll have access to some overloaded and generic versions of those methods. For instance, for the Resolve method, I used this version:
More
Posted by Peter Vogel on 07/17/20120 comments
Up until a few weeks ago, the only way I knew of to get back to an old command in the Command Prompt was to press the Up arrow and walk back through the commands. It turns out that if you press F7 in the Command Prompt, a window pops up showing all the commands you've executed, and you can cycle through them with the Up or Down arrows (you can't edit the commands, though).
Speaking of editing commands: The most common error I make when entering commands is misspelling file or folder names. It turns out that I don't have to type in a full file name in the Command Prompt. If I type my command, followed by the first letters of the file or folder name I want and just keep hitting the Tab key, I'll cycle through all the file and folder names in the directory. I can even use wildcard characters (e.g. c*.DLL to find all the DLLs with names beginning with the letter C) with this trick.
More
Posted by Peter Vogel on 07/08/20125 comments