Visual Studio Hacks [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Visual Studio Hacks [Electronic resources] - نسخه متنی

Andrew Lockhart

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







Hack 15. Use and Share Code Snippets

Tap into a whole universe of other
people's code and share some of your own.

Visual
Studio 2005
introduces a new feature called Code Snippets that allows you to
insert a piece of example code directly into the class you are
working with. At the time of this writing, this feature is available
only for Visual Basic.


2.11.1. Insert Code Snippets


You can quickly
insert example pieces of code by right-clicking in the area where you
want to insert code, then clicking the Insert Snippet item on the
context menu that appears. A list of code categories appears, as
shown in Figure 2-39.


Figure 2-39. Code Snippet categories

After you select the category Accessing Data, you will see the list
of examples that you can automatically insert, as shown in Figure 2-40.


Figure 2-40. Accessing Data example list

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?


2.11.2. 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 Studio 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 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 Studio 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 editing experience .


/ 172