Hack 7. Make Pasting into Visual Studio Easier![]() text. You can paste strings into Visual Studio as comments, string, StringBuilders, and more.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 2-4. Figure 2-4. SmartPaster menu![]() 2.3.1. Paste as String/StringBuilderI 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: SET ROWCOUNT 10then paste the code into Visual Studio using Paste As String, you would see the following code: @"SET ROWCOUNT 10" + 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);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. 2.3.2. Paste as CommentJust as with strings, any text on the clipboard may be pasted as a block of comments. I've found this very helpful in many cases. Having instant blocks of comments makes development much easier because of the following reasons:Business rule requirements may be pasted directly into the code for easier translation and to explain what is being done.Documentation from MSDN or other sources may be pasted in, avoiding the need to switch between the code and a browser window.When upgrading code from another platform, the legacy code can be pasted as a comment, making it easier to ensure the logic is the same. To paste text as a comment, you simply need to copy text to your clipboard and then choose Paste As if you copied the following piece of text to your clipboard: Call the test method to walk through this scenario and test every part.and then pasted it into your document using Paste As Comment, you would see the following comment added: //Call the test method to walk through this scenario and test every part. 2.3.3. Paste as RegionWhen pasting as a region, the clipboard text will simply appear between #region and #endregion tags with a region name of your choice. This feature is often helpful when organizing code within your application or pasting regions of code developed by someone else.First, copy a piece of code like this one to your clipboard: private void DoSomething( )Then select Paste As shown in Figure 2-5. Figure 2-5. Region dialog![]() use; after you click OK, this code will be pasted into your document: #region DoSomething Method 2.3.4. ConfigurationSmartPaster offers a number of configuration options to make the add-in work best for you. The SmartPaster configuration dialog is shown in Figure 2-6 and can be accessed through Paste As Figure 2-6. SmartPaster Configuration dialog![]() options, and most options are self-explanatory. 2.3.4.1 Line breaksThere are quite a few ways to specify line breaks: ControlChars.NewLine, Environment.NewLine, Char(13), and depending on your language, vbCrLf, \n, and \r\n. Most of these accomplish the same thing: insert the special ASCII characters CR (carriage return), LF (line feed), or both.The recommended way of adding line breaks is with Environment.NewLine. Unlike the other methods, this will insert the appropriate ASCII representation of a line break: an LF for Unix, a CR for Apple, and a combination of the two for Windows.However, practicality often supersedes portability, and for most cases, escaped carriage returns (i.e., \r and \n) are the absolute simplest to use. SmartPaster allows you to easily configure which option to use. 2.3.4.2 AppendFormat on StringBuilderThe AppendFormat checkbox determines how control characters should be handled within a string pasted as a StringBuilder. While this is mostly a matter of preference, one factor to consider may be performance. A string literal concatenated with another constant (such as "Hello World" & vbCrLf), will be combined at compile time, thus creating only one interned string. The AppendFormat method uses a formatter to parse the string and arguments such as "Hello World{0}", vbCrLf at runtime, incurring a slight overhead. 2.3.5. Key BindingsWhen installed, SmartPaster adds five configurable environment commands, one for each method of pasting, and the final to open the configuration dialog. These commands can be bound to a key combination [Hack #24] or placed easily on a taskbar to make the options work best for you.Alex Papadimoulis |