Subscribe Now

ABC, 123, Ruby, C#, SAS, SQL, TDD, VB.NET, XYZ

Wednesday, October 31, 2007

Your Friends, The Visual Studio Code Snippets

There are some great and well-known third-party tools that enhance or complement Visual Studio .NET, such as NUnit and Roeder's Reflector. Visual Studio's Code Snippets, both the default (built-in) snippets, and the C# and VB.NET add on snippets are amazingly useful and seemingly under-advertised and under-utilized. I don't hear many people talking about them and I haven't seen much use of them.

What are code snippets? Code shortcuts for writing bigger chunks of code. A quick and powerful example is a good way to introduce them.

Here's the default code snippet "prop" in action in C# that codes up an entire read/write property complete with private storage variable. They way I use this is instead of writing everything out by hand to code out a property, I simply type "prop" followed by TAB, TAB. As you can see, prop even appears in Intellisense.

prop code snippet in Intellisense dropdown list

After I hit TAB the 2nd time, I see this.

prop code snippet fills in a lot of code for you

Then I just type a quick "string", TAB, TAB, "myName", TAB, "Name" and I'm done!

quick product of using the prop code snippet

Will I ever type out another private member/getter/setter property combo again? I doubt it.

The official documentation says, "The Code Snippet Inserter is invoked through the Insert Code Snippet or Surround With commands on the IntelliSense menu, or by using the keyboard shortcuts CTRL+K, then X and CTRL+K, then S respectively." I find it easiest to just type out the snippet's keyword and press TAB, TAB. Things are a little different in the VB.NET editor. Snippet keywords don't appear on Intellisense lists, but simply right-click and select Insert Snippet.

in VB.NET right-click and select Insert Snippet

After clicking Insert Snippet, a categorized menu of snippets is presented.

snippet menu

Selecting Common Code Patterns and then Exception Handling and shows the available snippets in that category.

Exception handling code snippets

Selecting Try...Catch...Finally...End Try Statement inserts this code.

Try...Catch...Finally...End Try code snippet

Now I just need to decide what class of Exception I want to catch and I'm off and running.

To add on more snippets from MSDN or make and add your own, use the Code Snippets Manager.

Code Snippets Manager

For example, adding the Database code snippets gives you nice shortcuts such as "adoCreateSqlConn" which generates the following code.

System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
conn.ConnectionString = @"Data Source=ServerName;Initial Catalog=Northwind;Persist Security Info=True;User ID=;Password=";

I hope this was helpful. Enjoy!

Monday, October 29, 2007

Microsoft .NET (2.0) FTP Support Still Sucks

An FTP client is coaxed out of the .NET 1.1 framework by Liberty (& Enterprise Distributed Technologies –LOL) and Howard Richards shows how to get the job done with .NET 2.0. Whidbey (2.0) was billed as having FTP support, but when you have to write this code does that qualify as support?
string URI = "";
System.IO.FileInfo fi = new System.IO.FileInfo(@"c:\temp\deleteme.txt");
System.Net.FtpWebRequest ftp =
ftp.Credentials = new System.Net.NetworkCredential("anonymous", "");
ftp.KeepAlive = false;
ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;
ftp.UseBinary = true;
using (System.Net.FtpWebResponse response = (System.Net.FtpWebResponse)ftp.GetResponse())
using (System.IO.Stream responseStream = response.GetResponseStream())
using (System.IO.FileStream fs = fi.OpenWrite())
byte[] buffer = new byte[2048];
int read = 0;
read = responseStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, read);
} while (!(read == 0));
catch (Exception)

It almost makes more sense to shell out to c:\windows\system32\ftp.exe as in this VBA code.
Dim ff As Integer
ff = FreeFile
ff = FreeFile
Open "c:\temp\ftpcommands.scr" For Output As #ff
Print #ff, "open"
Print #ff, "anonymous"
Print #ff, "no_password"
Print #ff, "cd gnuftp/gnu"
Print #ff, "binary"
Print #ff, "get README.DESCRIPTIONS c:\temp\deleteme3.txt"
Print #ff, "bye"
Close #ff
ff = FreeFile
Open "c:\temp\ftpcommands.bat" For Output As #ff
Print #ff, "ftp -s:c:\temp\ftpcommands.scr"
Print #ff, "Echo ""Complete"" > c:\temp\ftpcommands.done"
Close #ff
Shell "c:\temp\ftpcommands.bat", vbHide
Do While Dir("c:\temp\ftpcommands.done") = ""
Dim dtmContinueAt As Date
dtmContinueAt = Now + TimeValue("0:00:03")
Do While Now < dtmContinueAt
Kill "c:\temp\ftpcommands.scr"
Kill "c:\temp\ftpcommands.bat"
Kill "c:\temp\ftpcommands.done"

Now contemplate this equivalent Ruby code.
require 'net/ftp''') do |ftp|
files = ftp.chdir('gnuftp/gnu')
ftp.getbinaryfile('README.DESCRIPTIONS', 'c:\temp\deleteme2.txt')

I feel prompted to ask, just wtf does Microsoft have against FTP? Aside from that, the obvious question is “will .NET 3.x ‘Orcas’ do better?” Or maybe the obvious question is, “is .NET a misnomer*?”

* Because last time I checked FTP was an Internet protocol: “a system for transferring computer files especially via the Internet.”

First Post

Dear readers, welcome to my blog. This is the first of what I hope will be many posts.

You may be wondering...what is “technoyoga”? I chose the name of this blog because I am interested in and practicing both computer technology and spirituality (among other things). A large bulk of my time is spent working in the computer programming arena as well as the spiritual practice domain. And who says they can't co-exist? What programmer hasn’t experienced the meditative bliss of an n-hour long coding binge in which the ego subsides and is replaced by classes, methods, unit tests, and compilations? I think a lot of programmers out there know what I’m talking about even if they haven’t put it in such terms.

I was introduced to the term “karma yoga” by Ram Dass many years ago and quite like the practice, hard as it is. As a blogger, I-I will strive to jab and throw haymakers on a regular basis without expectation and have some fun. I figure if Scott Hanselman can have his Computer Zen, then I can have my technoyoga. :)

Please note I am not affiliated in any way with or the Techno Yoga Mat. Hopefully they won’t ask me to change the name of my blog.