Visual Basic for Very Bright Kids - Part 1 - Getting Started

February 2007

Are We Allowed To Have Fun Here ?

I suspect I know what you’re thinking … "I want to write a program and I want to write it now! Coding is the cool part; I don’t want to read half the book before I can write my first program. In fact, this sentence is getting too long already - I want to get going!"

Bb330917.a41be095-9273-46a8-9154-1f9138ead0c2(en-US,VS.80).png

Truthfully, I’ve yet to meet a programmer who can resist diving in – after all, if you bought a new bike, would you sit down and read the book first? Quite likely not – you’d leap aboard, take it for a spin, fall on your face and only later discover the fact that the brakes on this model are opposite to the usual conventions.

So let’s make a deal. We’ll dive right in within this first chapter. Once you’ve installed the software, you can run some of the sample programs and, if you're feeling brave, perhaps even try modifying them.

But the code will look strange and won’t make much sense – you’ll be riding blind – and when you make a small mistake you’ll be clueless as to what’s wrong and how to fix it. It just won’t work and you won’t know why. That’s when you know it’s time to start studying the theory like a pro. Don't become discouraged when something doesn't work, and give up. When that time comes, read the rest of the book; okay?

The Really Important Part

In programming, as in all other professions, the people who achieve the most are those who love what they do. If you enjoy your programming, you’ll find it hard to stop yourself learning new things. Your need to know will drive you forward and studying further will just be something that happens along the way.

So, above all, enjoy yourself as you master the world of Visual Basic programming!

Bb330917.ac4aad46-612b-4d2f-a3f1-f5911c73f9d4(en-US,VS.80).png

Software You'll Need

To develop Visual Basic programs, you’ll need :

  1. The Microsoft .NET Framework 2.0, which is available, free, from https://msdn2.microsoft.com/en-us/netframework/aa569263.aspx.

  2. A .NET development environment. We recommend Microsoft Visual Basic 2005 Express Edition, which is available free from Microsoft at https://msdn.microsoft.com/vstudio/express/vb/ . The Visual Basic 2005 Express installation package includes the .NET Framework so you don’t need to worry about installing item #1 above separately.

  3. Lastly, you need to copy the book's example files to a drive on your own computer. Extract the file example.zip to any folder of your choosing. Just remember which location you choose since you’ll be asked to open the files from there later.

More About Microsoft Visual Basic 2005 Express Edition

It is possible to create Visual Basic programs using nothing but a simple text editor (like Notepad) and something called the "Visual Basic compiler" included with the .NET Framework. To make things a little easier though, and to make sure you can carry on further once you've read this book, we suggest you use a development environment such as Visual Basic Express. It is a lightweight environment for developing Windows applications and console (command-line) applications, using Visual Basic.

It's important that you understand that this book is not about teaching you about the Visual Basic Express Edition software product - rather it teaches you about the Visual Basic language. Visual Basic Express also has lots of snazzy features like writing Visual Basic code for you when you drag-and-drop a button or other control onto the page. That's cool and you're welcome to use it - but this book aims to teach you Visual Basic "from the ground up". We'd like you to learn to write Visual Basic code by hand first - so that you really understand what's happening.

If you want to learn more about the Visual Basic Express development environment, go to the website at https://msdn.microsoft.com/vstudio/express/vb/ .

Writing a New Program Using Visual Basic Express

  • To start Visual Basic Express, click START -> All Programs -> Microsoft Visual Basic 2005 Express Edition

  • To start a new project, simply select File -> New Project and then choose a project type. We will start with some Console applications and will later move on to Windows applications.

    Bb330917.99b5c55e-eb41-40b3-a999-cc8702df80bb(en-US,VS.80).png

  • For example, try creating a new Console Application and clicking OK. By default a file Module1.vb is created. Rename the file to Program.vb. When the coding window for Program.vb appears, delete all the code that is automatically inserted and type a simple program such as this. You can copy and paste it if you're feeling lazy. Or is that smart?!

    Imports System
    
    Class PleaseSayYo
          Shared Sub Main()
             Console.WriteLine("Yo!")
             Console.ReadLine()
          End Sub
    End Class
    

Make sure you type it EXACTLY as above (except that formatting like italics can be ignored).  

  • Then click the Run or Play button (or press F5)

    Bb330917.56d66a34-7f8c-448b-8d18-1b4f846fd6e7(en-US,VS.80).png

  • If the program runs successfully, it will appear after a few moments, in a window above the Visual Basic Express window.

    Bb330917.fd5727b7-06a0-4ab3-8329-4b5bd2337bbf(en-US,VS.80).png

    When you're done, you must stop the program. Usually, you will do this by clicking the X in the top right-hand corner to close the window.

  • If there is some problem in the code of the program, you'll be alerted to this fact. For example, you notice that in the Visual Basic computer language, we must type each statement on a new line. If you try putting more than one statement on one line and then clicking the start button, you'll see the error message below.

    Bb330917.9592c2a5-58d2-4de8-8354-069a3ffba60a(en-US,VS.80).png

    In most cases, you will click "No", and will be shown some details in the "Error List" window, including an indication of which line number the error was found on. If you then double-click that error description, you will be taken to the line where the problem exists, so that you can try to correct it.

    Bb330917.192283fc-ae19-4fa1-9480-9da98a6d73d2(en-US,VS.80).png

  • Sometimes a statement is too long. But Visual Basic needs each statement to be on a separate line. In this case, you can use underscore "_" to break up a statement. For example, if you have a long statement like the one shown below:

    MyButtonClickEventHandler(ByVal sender As Object, ByVal anEvtArgs As EventArgs)
    

    And you want to show it on two separate lines. You can do so as follows:

    MyButtonClickEventHandler(ByVal sender As Object, _
                            ByVal anEvtArgs As EventArgs)
    

Running the Book's Sample Programs

Several of this book's example programs need accompanying files. Those programs won't run correctly if you simply paste the code in as above.

So once you've copied the example files to some folder on your computer, you should run them by:

  • In Visual Basic Express, select File -> Open Project

  • Browse to the folder where you saved your example files, and select the project you want, which will always have a .vbproj (Visual Basic project) extension.

  • Click the green Run button.

Formatting Standards Used in this Book

In order to make it easier to follow the code samples in the book, we have generally stuck to certain habits:

Consider this block of code as an example:

' Main method
 
Shared Sub Main()
      Application.Run(New MyButtonClass())
End Sub
 
' Event handler method
 
Sub MyButtonClickEventHandler(ByVal sender As Object, ByVal e As EventArgs)      mrButton.Text = "You clicked me!"End Sub
  • Code samples are written using a slightly different font to the rest of the text (Courier New)

  • Any words or symbols that we "choose ourselves" are written in italics. Examples above include "MyButtonClass" and "mrButton".

  • Any words or symbols that are part of the Visual Basic language or built into the .NET Framework, such as "Shared Sub Main()" or ".Text", we write with normal style.

  • Bold text simply indicates something we want to draw your attention to.

  • A single quote ' indicates comments to explain the code (actually this is a feature of the Visual Basic language). Anything on the line beyond it is ignored by the computer. An example above is "' Main method".

Part II of the book contains most of the general concepts and we often summarize the key concepts there. These summaries are called "Building Blocks". Here's a partial example showing how a Building Block appears.

Building Block : Classes

All Visual Basic programs are built inside classes. The basic structure of a class is written down for the computer as follows :

Class Animal

End Class

What's Next?

You will probably want to play with Visual Basic Express for a while - you may wish to explore its help files and see what it's all about. If you're new to Visual Basic, though, you'll probably soon find that having a programming tool is not enough - you need more than just a tool in order to communicate with the computer.

Bb330917.928e713a-d25d-4cdc-913c-a436a664e8a3(en-US,VS.80).png

Then it's time to tackle the next part of the book - learning how to communicate with the computer using the Visual Basic programming language.