The magazine of the Melbourne PC User Group
Seeing the Basics: Did you know? (Part 1)
Tony Stevenson |
|
Visual Basic 5.0 is a great product, but also an extensive, and sometimes
complex one. Even for accomplished VB developers, there's always something new to learn or a programming
mystery to unravel. So, this month's column looks at some different and interesting aspects of the language,
presented under the general title of "Did you know?"
Grouping objects alphabetically
Did you know: It is recommended to use a three-character prefix to name objects and variables you use
in your programs. For example, the prefixes "CBO", "TXT", and "CMD" are conventionally used to name combo
boxes, text boxes, and command buttons respectively. To find out more about them, most VB books are likely to
contain a table listing the full set. There are two advantages to using these prefixes. Firstly, it makes it
easier to debug programs because you know exactly what sort of object is being referred to. And secondly,
when you use the Object list box in the Code window, all the objects will be listed in alphabetical order, as
well as conveniently grouped together by type.
Fast access to VB's set of toolbars
Did you know: You can display the various VB toolbars, for example, the Edit, Debug, or the Form
Editor toolbars, by right clicking with your mouse on an empty section of the VB main menu bar. And once any
toolbar has been displayed, you can then right click on its title bar to get quick access to the rest of
them.
A brief history of Basic and Visual Basic
Did you know: The Basic programming language was created in 1963 by John Kemeny and Thomas Kurtz, two
professors at Dartmouth College. Its name is really an acronym for Beginner's All-purpose Symbolic
Instruction Code. Then in the 1970s, Bill Gates adapted it for use on PCs. In fact, it was in October 1975
that Microsoft Basic was released for 4 KB and 8 KB computers. The language then evolved into different
versions such as Microsoft QuickBasic and MS-DOS QBasic. However, in mid-1991, the way in which Windows
applications were to be programmed was irrevocably altered with the first release of Microsoft's Visual
Basic.
Leaving debug statements in a VB application
Did you know: The Debug.Print statement can be a handy way to debug a tricky part of a VB application.
The output from this statement is sent to the Immediate Window which you can look at to see what is happening
internally within your program. To display the Immediate Window when you're in VB Design mode, click View on
the main menu bar followed by the Immediate Window option (alternatively use the keyboard combination of
Ctrl+G). However, once you've got your application running correctly, and you're ready to create an
executable version of it, there's no need to physically remove the Debug.Print statements from your code.
They simply will not appear when your program is run from a compiled executable because they will have been
automatically de-activated during the compilation process.
Gaining the right image
Did you know: The Image control's Picture property can accommodate the following six types of
graphics files: bitmaps (.BMP files), Windows metafiles (.WMF files), icons (.ICO files), cursors (.CUR
files), JPEG format (.JPG files), and GIF format (.GIF files). If you attempt to load an image control with a
graphics file which doesn't correspond to one of these formats, for example, a file in TIFF format, your VB
program will crash. The runtime error message will inform you that an invalid graphics format was assigned to
the Picture property of the image control. One possible work around in a situation like this is to convert
the TIFF graphic into another format, for example, into JPEG format. This can easily be achieved using a
graphics program like Paint Shop Pro which is available as shareware. Note however that depending upon the
contents of the graphic file being converted, you might sometimes experience some loss in its quality or
colour intensity. And finally, if your application allows users to dynamically select graphics when the
program is executing, bullet-proof your code to ensure that any graphic selected corresponds to one of the
six acceptable formats listed above.
Improved readability
Did you know: It's theoretically possible, if you so desire, to use more than a thousand characters in
any line of code in your program (the upper limit is actually 1023 characters). However, if you do so, you or
anyone else who has to maintain your program will be constantly scrolling the screen to see the remainder of
each line. It is far more advisable to limit the number of characters you use to about 50, and have a
self-imposed absolute upper limit of 70 characters. By using these sorts of limits, as well as ensuring the
code window is expanded to take up as much screen real estate as possible, you'll be able to comfortably look
at, and edit, your programs. Nevertheless there will be those occasions where you'll want to split a logical
line of code over two or more physical lines to improve readability of your program. This is easily done
using Visual Basic's line continuation character, the underscore ( _ ), at the spot in the line where you
want the split to occur. Note however the line continuation character cannot be used to split up a string
contained within quotation marks, or to break up a Visual Basic keyword.
More interesting facts about VB
Next month's Seeing the Basics will continue with part two of "Did you know?" about MS Visual
Basic.
Reprinted from the February 1998 issue of PC Update, the
magazine of Melbourne PC User Group, Australia
|