Visual Studio Add-Ins: Paste As…

If you are like most programmers, you tend to fiddle with your SQL in some tool where you can run, test, and optimize your SQL before you paste it into your app. Or perhaps you have some long static text info (such as instructions) that you need to display to the user. You type it all in, then you have to mess around with getting it onto a string. You break it into multiple lines, perhaps you are using a string builder to hold it. The whole time you’re thinking there’s got to be an easier way. Well, there is!

The great add on, Paste As… from

http://weblogs.asp.net/alex_papadimoulis/archive/2004/05/25/Smart-Paster-1.1-Add-In—StringBuilder-and-Better-C_2300_-Handling.aspx (or if you like shorter URLs, I’ve shrinksterized it, http://shrinkster.com/jut ).

This add-on takes whatever text you have in the buffer and pastes it into your code in one of four ways: as a comment, as a string, as a string builder, or as a region. Let’s take a fake SQL statement and render it the various ways. First, here is a SQL Statement that you’ve worked on inside your SQL tool (it’s fake, but you’ll get the idea.)

select FirstName, MiddleInit, LastName, Street, City, State, Zip, PayRate, HireDate from SomeEmployeeTable order by LastName, FirstName, MiddleInit

Here it is as a comment:

//select FirstName, MiddleInit, LastName, Street, City, State, Zip, PayRate,

//HireDate from SomeEmployeeTable order by LastName, FirstName, MiddleInit

Now here it is as a string:

@”select FirstName, MiddleInit, LastName, Street, City, State, Zip, PayRate, “ +

@”HireDate from SomeEmployeeTable order by LastName, FirstName, MiddleInit”

All you have to do is put “myString = “ on the front, and (if you are in C#) a ; at the end and you’re done. Now here it is as a StringBuilder. Note it will prompt you for the name of your StringBuilder variable, and how cool is it that it automatically calculates the size of the string for you (in this case 147).

StringBuilder myStringBuilder = new StringBuilder(147);

myStringBuilder.AppendFormat(@”select FirstName, MiddleInit, LastName, Street, City, State, Zip, PayRate, “);

myStringBuilder.AppendFormat(@”HireDate from SomeEmployeeTable order by LastName, FirstName, MiddleInit”);

 

The final option is Paste As Region. While you would not typically paste a single SQL statement as a region, let’s see how it looks anyway:

#region myRegion

select FirstName, MiddleInit, LastName, Street, City, State, Zip, PayRate, HireDate from SomeEmployeeTable order by LastName, FirstName, MiddleInit

#endregion

In the context we’ve presented above probably not something you’d do. Where this would come in handy is when you’ve copied a bunch of code to the clipboard, and want to paste it into a new region. Since it does no reformatting, this makes it an easy way to bring in new code to your application.

This tool works with both VS2003 and VS2005, and works in both C# and VB.Net. It has quickly reached the top of my “indespensible” list and is one of the first I install when setting up a new instance of VS. And it’s free! How cool is that.

Advertisement

One thought on “Visual Studio Add-Ins: Paste As…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s