Exercise 5 :: Printing and Reading code

You may have noticed that VB splits our code up into little chunks for us. This is part of the advantage of VB, it means that we do not need to try and think about the whole problem at once. Rather we can just find simple solutions to little problems and combine them to create a more complex program.

However, this leaves us with a little problem. What does my project look like if I print it up? More importantly, how can I read someone else's code and perhaps incorporate it into my own project. In this exercise we address these two issues.

Task One :: Printing your program

In this task, we will generate a printout of one of our own projects

  1. Open the calculator project you created in exercise 3.
  2. Go to the File->Print menu
  3. Ensure the options for "Code", "Form Image" and "Form as Text" are selected.
  4. Hit "Print"
  5. Go and get your printout from the printer.

What you get

You should have three pages. The first page will consist of all the code you have written for various events. In this case, this is just the code for "cmdCalc_Click()" and "cmdCalc_MouseMove". The second page will be a list of properties and associated values for each of the controls (plus the any forms they are on). Notice that not all properties are printed out. Only properties that differ from their default values get printed. The third page is an image of the forms that are in the project (in this case, just the one form).

Globals

Before we can get going on the next task, you need to understand globals. If you choose "(General)" from the object list in the coding window, you will be able to add/view global declarations. These are variable and procedure declarations that are available to everything else in that form. When you print out a project, the globals will be at the top of the first page, not in any functions.

Task Two :: Coding the easy way

In this task, we will learn how to incorporate other people's code into our own. We will take someone else's project, print the project out and then use this printout to help us improve our own calculator.

  1. Save a copy this VB project and then open it. If you have forgotten how to do this, re-read Exercise 2, Task 3 This is an enhanced version of the calculator program we have been creating.
  2. Run this program and identify its new features.
  3. Print out this project and then close it.
  4. Find the new sections of code and the new controls and add them to your project.
  5. Run your project to see if you have succeeded.
  6. Save your project.
  7. tip: having trouble working this one out? Jump over here for a full solution.

NEXT ->


Matthew Roberts, Macquarie University 2002