Now let us work with code snippets. Code snippets are new in Visual Studio 2005. They provide reusability. Their task oriented functionality included inside the box plus you can create your own snippets and share them with the team. You also have the ability to import community code snippets. As we showed you earlier, an undocumented feature of the toolbox is the be able to drag and drop code in and out of it. Something like this, “Highlight a PC code and drag it over to your toolbox.” We showed you earlier that these snippets can be dragged back into your code giving you reusability. This was an undocumented feature and previous versions of Visual Studio and still exist today.
There is one big problem with this. In the event that the machine failure or situation where you have to reinstall Visual Studio, you will lose all these snippets. There is no database to store these snippets and they are not quite scalable. What is the better way to work with snippets?
Visual Studio 2005 had introduced the snippet database that allows you to destroy the snippets in a reusable fashion that is preserved as long as you them to be. Go ahead and delete this code. You can invoke snippets by right clicking on the editor and selecting insert snippets. This pops up a categorized view of the snippets that I have available for Visual Basic. I can see that I have quite a variety of snippets that come out of the box. For example by scrolling down into my math component, I can see that I have a variety of math functionality such as calculating a monthly payment on a loan. Go ahead and select that.
As you can see, the code snippet was automatically generated for me. Another great feature in Visual Studio 2005 is the fact that the snippet will highlight elements that I need to input. For example, if I harbor over thirty-six, I can seen that I can replace this code with a value that returns that the total number of monthly payments. Go ahead and delete this. Here is a tip for you. A handy way to bring up the snippet editor without moving your mouse is to go ahead and type question mark and then select TAB. You obtain the same result.
Let us experiment with some more math functionality. Select math, you could see that it can also generate a random number using the random class. That way I do not have to dig around looking for the class that I need. And I can go ahead and advantage of this commonly used functionality. Select Ctrl+Z to undo this. Let us look some other snippets. Right click, insert snippet, this time let us look at some common code patterns. Here I can see conditionals and loops, exception head link, properties and types. Let us select the first one. Here I see a list of conditionals and loops.
Let us go ahead and select one such as the For Each next statement. This generates the shaft for me to navigate through items in a loop. Hit Ctrl+Z to undo this. Right click and insert a new snippet. Let us look at some of the data functionality. Select data, this gives me access to commonly used features in ADO.net. Let us say I want to go ahead and create a server connection to a sequel server. If I select this option, you can see that it automatically generates the connection string for me. All I have to do is still on the database and the server name and that is it. I have a connection object.
Select Ctrl+Z to Undo. Let us work with pile system operations. Insert the snippet, this time let us use the question mark Tab option. Select file system and in this example, I can create a file on my machine. Let us insert another snippet. I am in a windows form. So let us see what kind of functionality we have available for windows forms. Scroll down, you will see windows forms application. You can work with fonts, you can work with forms. Let us go ahead and select the clipboard in this example. Let us say I want to get an image from the clipboard. Let us select that. Now, what are these two functions have in common? If you look, you will see the MY keyword. For those of you who knew the Visual Studio 2005, you will know that this is the new name space
Transcription by:
Scribe4you Transcription Services