Use and Share Code Snippets #15
Chapter 2, Master the Editor
|
71
HACK
You can then select the “Create a DataTable with columns and a primary
key” snippet, and the following code will be inserted into the class:
Private Sub CreateTable( )
Dim IDColumn As New DataColumn("ID")
IDColumn.DataType = GetType(Integer)
IDColumn.AutoIncrement = True
Dim FNameColumn As New DataColumn("FirstName")
FNameColumn.DataType = GetType(String)
Dim LNameColumn As New DataColumn("LastName")
LNameColumn.DataType = GetType(String)
Dim EmployeeTable As New DataTable("Employees")
EmployeeTable.Columns.Add(IDColumn)
EmployeeTable.Columns.Add(FNameColumn)
EmployeeTable.Columns.Add(LNameColumn)
EmployeeTable.Constraints.Add("Key1", IDColumn, True)
End Sub
Of course, this code probably does not do exactly what you want it to do,
but it gives you a great start to work from. Code Snippets are a great way to
incorporate examples into your everyday coding, but wouldn’t it be great if
you could build your own library of code snippets?
Add Your Own Code Snippets
A large number of code snippets are already loaded into Visual Studio, but
you can also add your own. This could be very useful if you want to create
snippets for common pieces of code you use or code that is specific to your
project or architecture. You will also be able to download and add code
snippets from community sites as well; I have a feeling that it will become
common for people to post snippet files on their sites and blogs. To add or
modify code snippets, you need to first create a snippet file. For Visual
Basic, this is a structured file with the extension of .vbsnippet.
The creation and editing for the .vbsnippet files is not complete in the Beta 1
release of Visual Studio 2005, so hopefully this will be easier in the future.
For now, the best way to create new snippet files is to start with an existing
snippet file. These can be found in the Visual Studio Directory\Vb\Snippets\
1033 directory. First, you need to make a copy of an existing snippet file,
then open the copy in Visual Studio. At the time of this writing, Visual Stu-
dio will treat this file as a solution file and provide a number of property
windows to set the name of the snippet as well as the author and other
metadata. After entering your own snippet, you can save the modified file
72
|
Chapter 2, Master the Editor
#15 Use and Share Code Snippets
HACK
and then add it to Visual Studio using the Code Snippets Manager located
under the Tools menu in Visual Studio 2005.
There won’t be a complete editor included in the final version of Visual Stu-
dio 2005, but the team plans to release a separate application at or around
the release of Visual Studio 2005 that will provide a rich code snippet edit-
ing experience.

Get Visual Studio Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.