In this chapter, we will use the Code Snippets feature of Visual Studio to insert source code into a basic sample code framework. Code snippets is a new Visual Studio feature that is basically a hierarchy of saved source code text. If you have installed the code for this book, you will find code snippets for most chapters included right in Visual Studio. In this chapter's project, I will show you how to use them to add chapter-specific code into your project.
Because we haven't officially started the Library Project, this chapter's project will simply extend the "Hello, World!" project we developed in Chapter 1, but with fun parts added. I will include some of the language features we discovered throughout this chapter.
Load the "Chapter 2 (Before) Code" project, either through the New Project templates, or by accessing the project directly from the installation directory. To see the code in its final form, load "Chapter 2 (After) Code" instead.
Each chapter's sample code includes a "Before" and "After" version. The "After" version represents the code as it will look when all the changes in that chapter's "Project" section have been applied. The before version doesn't have any of the chapter's project changes included, just placeholders where you will insert the code, one block at a time.
Like the project in Chapter 1, this chapter's project includes a basic Windows form with a single button on it. Clicking on the button displays the same "Hello, World!" message. However, this time, the message starts in an encoded form, and a separate class decodes the message and triggers an event that displays the form.
Once the project is open, view the source code attached to Form1. It should look somewhat like the following.
Public Class Form1 ' *** Insert Code Snippet #2 here. Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click ' *** Insert Code Snippet #3 here. End Sub ' *** Insert Code Snippet #4 here. End Class ' *** Insert Code Snippet #1 here.
This sample uses a separate class to process the displayed message. The code for this class appears as snippet number 1. To insert the snippet, move the cursor just after the #1 snippet marker line, which reads:
' *** Insert Code Snippet #1 here.
To insert a snippet through the Visual Studio menus, select Edit IntelliSense Insert Snippet. The equivalent keyboard sequence is Ctrl+K, Ctrl+X. Or type a question mark (?) anywhere in the source code, followed by the Tab key. Any of these methods displays the first level of snippets (see Figure 2-4).
Figure 2-4. Snip, snip, snip
From the snippet list, select Start-to-Finish Visual Basic 2005, and then select Chapter 2. A list of the available snippet items for this chapter appears (see Figure 2-5).
Figure 2-5. Item, item, item
Finally, select Item 1. The content magically appears within the source code. All insertions of code snippets throughout this book occur exactly this way.
Snippet 1 inserts the SayHello class, part of the HelloStuff namespace, a portion of which appears here.
Namespace HelloStuff Friend Class SayHello Private secretMessage As String Private reverseFlag As Boolean Private decoded As Boolean Public Event MessageDecoded( _ ByVal decodedMessage As String) Public Sub New(ByVal codedMessage As String, _ ByVal reverseIt As Boolean) ... Public Sub DecodeMessage(ByVal rotationFactor As Integer) ... Public Sub ReportMessage() ... End Class End Namespace
The SayHello class includes three private fields (secretMessage, reverseFlag, and decoded), which monitor the current status of the display message. A constructor (New) allows the user to create a new instance of SayHello with an initial message text, and a flag that indicates whether the text should be reversed before display. The DecodeMessage subroutine converts each letter of the encoded message to its final form by shifting each letter a rotationFactor number of places. If the letter "E" appears, and rotationFactor is 3, the letter E is shifted three spaces forward, to "H." A negative rotation factor shifts the letters lower in the alphabet. The alphabet wraps at the A-Z boundary. Only letters are rotated, and upper and lowercase are handled independently.
The ReportMessage method fires the MessageDecoded event, sending the previously decoded message to the event as an argument. So where is this event handler? It's attached to an instance of SayHello that will be added to the Form1 class.
Insert Chapter 2, Snippet Item 2.
Private WithEvents HelloDecoder As HelloStuff.SayHello
The HelloDecoder class is an instance of the HelloStuff.SayHello class that we just wrote, and the snippet makes it a member of the Form1 class. The WithEvents keyword says, "This instance will respond to events;" specifically, the MessageDecoded event from the SayHello class.
Let's add the code that triggers the message to display when the user clicks on the on-form button. This occurs in the button's click event.
Insert Chapter 2, Snippet Item 3.
HelloDecoder = New HelloStuff.SayHello("!iqwtB, tqqjM", True) HelloDecoder.DecodeMessage(-5) HelloDecoder.ReportMessage()
These three lines create an instance of the SayHello class, storing it in the HelloDecoder class field. Can't read the first argument in the constructor? It's encoded! It's a secret! And the True flag says that it's been reversed to make it an even bigger secret (you don't know what it is!). The DecodeMessage removes the secrets by shifting each letter as needed, although the reversal doesn't happen until the call to the ReportMessage.
The ReportMessage method doesn't actually display the message. Instead, it fires an event that makes the unscrambled message available to an event handler.
Insert Chapter 2, Snippet Item 4.
Private Sub HelloDecoder_MessageDecoded( _ ByVal decodedMessage As String) _ Handles HelloDecoder.MessageDecoded ' ----- Show the decoded message. MsgBox(decodedMessage) End Sub
The Handles keyword connects the subroutine with the fired event. The decoded message comes into the handler through the decodedMessage argument, and is splashed all over the screen with a simple yet powerful call to the MsgBox function.
That's it for the sample code. Now it's time to roll up your sleeves and embark on a full Visual Basic 2005 project.