Creating and using code fragments
Code fragments (synonym: snippet) are pieces of code that occur regularly in programs and for that reason have been collected for reuse.
Code VB includes a code fragment library which you should extend with code snippets which have proven useful to you.
Inserting a fragment
Code fragments always appear at the top of the sub menu. Code fragments don't have shortcut keys and for this reason must be selected using down-arrow and Enter or using the mouse.
Click this to see how to add fragments
You will also want to extend the library with your own fragments. This is discussed in the following sections.
Creating your own code fragments
When you intend to create a code fragment of your own, the first question is where will be the best place to store it. What location in the menu will be most likely to look for it. As an example, we take the Switch fragment which is not yet included. The switch statement is a compact way to relate an output value to a given input thus specifying a relationship using code. In this example the function matchLanguage can tell us what language is spoken in Rome. Because it resembles the Select Case statement, I decide to put it there.
Function matchLanguage(ByVal cityName As String) As String matchLanguage = CStr(Switch( _ cityName = "London", "English", _ cityName = "Rome", "Italian", _ cityName = "Paris", "French")) End Function
A fragment is essentially just some VB code, optionally with some fragment tags.
The example fragment below illustrates things you can do with fragments. Press < to open a selection list of available tags. Select an item and press Enter.
<cursor>; lets you determine where the cursor should appear after the fragment is inserted. If not specified the cursor will be at the end of the fragment
<Date> Tells the fragment insertion to try to find a variable of the selected type (here Date) or insert a new variable. If more than one variable of this type is found only a prefix is inserted, allowing the user to use intellisense to select the appropriate variable
<String:File> Adding a name after : inside the tag allows you to propose a filename in case a new variable is to be created - in this example the variable name will be strFile.
<Range> Alternatively, you can construct a tag with a typename yourself.
<include> An advanced use allows you to specify a module to be inserted when using the fragment. In the case below the module contained the Windows API call for getting the Temp folder.
Managing code fragments
In addition to creating fragments you will also want to edit, copy or remove them. You will be able to do all these using Windows Explorer. Using the Manage Fragments on the menu where the fragment you want to to work on opens Explorer in the correct folder location.
|The code fragments are stored in the \My Documents\VB Code\ folder|
Edit an existing fragment
To open an existing fragment for editing
- In the menu go to where the fragment is located
- Select Manage Fragements - use shortcut M
Windows Explorer opens in the folder containing the fragment
- Open the fragment using double-click or right-click » Open
Click this to see how to open an existing fragment for editing