Reusing Code: Making Things Easier
The only thing better than writing code is not having to write code—Visual Basic makes it easy to reuse code that is contained in Help topics and sample applications.
If you are using Visual Basic Express, some of the Help links on this page may be unavailable, depending on the options that you chose during installation. For more information, see Troubleshooting Visual Basic Express.
You may have noticed as you worked through the lessons that wherever there were blocks of code in the Help topics there were also labels marked Copy Code. Clicking the Copy Code label in a Help topic copies the code in that block to the Clipboard; you can then paste it directly into the Code Editor instead of typing it.
Many Help topics contain code examples to show how you might use a particular language element, property, or function. Although they may not match exactly what you want to do, you can copy them into your code as a starting point, and then modify them to fit your needs by changing variable names and references.
Visual Basic 2008 includes links to several sample applications that can give you a start in creating your own programs. Sample applications are complete projects that can be downloaded, loaded into Visual Basic 2008, and run without modification. In most cases they aren't very useful as programs; however, they often contain a lot of useful code.
For example, the Game sample application demonstrates a simple Windows Forms game, complete with GDI+ graphics, timer features, user configuration, and high-score storage. If you are writing your own card game, the user interface code in the Game sample probably won't be very useful. However, the code for storing and displaying high scores might be very useful; you might be able to copy it and use it in your program as is.
In other cases, you might want to take the sample application and use it as a starting point, modifying and adding to it to meet your own needs. Even if the sample doesn't do exactly what you want, it is often easier than starting from the beginning. For more information, see Visual Basic Express Sample Applications.
After you have been programming for a while, you will probably find that you are writing the same code repeatedly. For example, you might write code for a TextBox control to enable only numbers to be entered. Rather than writing the same code for every program, you can write the code one time and save it by dragging it to the Toolbox. The next time that you need that segment of code, you can drag it back onto the Code Editor to insert it in your code.
If you find that you are saving a lot of code to the Toolbox, you may find it difficult to find the code that you need. You can also save code as a Code Snippet and access it just like the Code Snippets that are included with Visual Basic 2008.
In addition to the code that is included in Visual Basic Express, there are many other sources for obtaining code, both from Microsoft and from other sources. You can search for code snippets, templates and starter kits, samples, and controls on the Internet. For more information, see How to: Search for Code Snippets Online.
You can also search Help to find topics that contain code. For more information, see How to: Find Topics with Sample Code.
In this lesson, you learned about several options for finding code so that you don't have to write it yourself. In the next lesson, you will learn where to find in-depth information about different features of Visual Basic Express.
Next Lesson: Going Further: I Want to Know More About…..