O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  

Buy the book!
Visual Studio Hacks
By James Avery
March 2005
More Info

Make Pasting into Visual Studio Easier
Don't be limited to plain text. You can paste strings into Visual Studio as comments, string, StringBuilders, and more
[Discuss (0) | Link to this hack]

SmartPaster is a plug-in for Visual Studio .NET 2003 that allows text on the clipboard to be pasted in a format compatible with C# and Visual Basic code. SmartPaster can be downloaded from http://www.papadimoulis.com/alex/SmartPaster1.1.zip. After downloading and installing SmartPaster, you will see a new item on the right-click (context) menu, which is shown in .

Figure 1. SmartPaster menu

Paste as String/StringBuilder

I find myself most frequently pasting text as a string or a StringBuilder. You can copy any sort of text from another application, then when you paste that text into Visual Studio, you can choose to paste it as a string or as a StringBuilder.

Many developers like to build a SQL statement using a tool such as Query Analyzer, for easy testing and debugging, or Microsoft Access, for quick, visual development. As simple as it is to build queries externally, putting them into code can often be a challenge, especially when the queries span multiple lines. SmartPaster eases the task of bringing external queries to code: simply copy your query to the clipboard and paste as a string or StringBuilder. For example, if you copied the following SQL to your clipboard:

SELECT ProductName
FROM Products
ORDER BY Products.UnitPrice DESC

then paste the code into Visual Studio using Paste As → String, you would see the following code:

@"SET ROWCOUNT 10" + Environment.NewLine +
@"SELECT ProductName" + Environment.NewLine +
@"FROM Products" + Environment.NewLine +
@"ORDER BY Products.UnitPrice DESC" + Environment.NewLine +

You could also paste this code using Paste As → StringBuilder and specify a StringBuilder name of "sqlBuilder," and this would result in the following code:

StringBuilder sqlBuilder = new StringBuilder(141);
sqlBuilder.AppendFormat(@"SET ROWCOUNT 10{0}", Environment.NewLine);
sqlBuilder.AppendFormat(@"SELECT ProductName{0}", Environment.NewLine);
sqlBuilder.AppendFormat(@"FROM Products{0}", Environment.NewLine);
sqlBuilder.AppendFormat(@"ORDER BY Products.UnitPrice DESC");

Like SQL statements, text displayed to the user is often developed externally, either by a copywriter, business analyst, or coder (such as myself), and requires the spellchecker within Microsoft Word. Usually, pasting such code would require going character by character, escaping quotes, and manually adding in line breaks. With SmartPaster, a quick right-click, paste-as, and your external copy is now internal without any of the normal hassle.

In an ideal world, all messages and dialogs would be stored in an external resource file and all SQL statements would be in views and stored procedures. But in a world of deadlines and disposable microapplications, doing it the right way is often trumped by "make sure it works."

As we've seen, SmartPaster offers the option of pasting your text as a string or a StringBuilder. While the difference may seem cosmetic, there are actually appropriate times to use one over the other. The reasoning behind this is that string literals (i.e., strings explicitly declared in your code, as opposed to those input by the user) are immutable. This means that every operation on a string, such as a concatenation or replacement, involves creating an in-memory buffer, performing the operation, creating a new string, and finally passing the old one to garbage collection.

Knowing that, it's fairly easy to decide whether to use a string or a StringBuilder. If the text will always be static, such as a tool tip, there will be no advantage to using a StringBuilder (even if string literals are concatenated across lines, the compiler joins them in memory). However, if the text will vary on conditions, such as a runtime error message, then there will definitely be a performance hit using a string as opposed to a StringBuilder.


SmartPaster offers a number of configuration options to make the add-in work best for you. The SmartPaster configuration dialog is shown in and can be accessed through Paste As → Configure.

Figure 3. SmartPaster Configuration dialog

A bit of explanatory text is available as a tool tip for all of the options, and most options are self-explanatory.

O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.