The magazine of the Melbourne PC User Group
Seeing the Basics: Did you know? (Part 3)
Tony Stevenson |
|
More interesting facts about VB
Last month's column looked at some different and interesting aspects of the Visual Basic language, presented
under the general title of Did You Know? It continues this month with Part 3.
Capitalising on your VB expertise
Did you know: Once you're a Visual Basic developer, you can then use your knowledge of this powerful
programming language in other rewarding ways.
For example, Microsoft products such as Excel, Word, Access, PowerPoint, and Project all contain a version of
VB, commonly referred to as VBA (Microsoft Visual Basic for Applications). And because you'll instantly
recognise many of VBA's keywords, functions, and so on, because of your VB experience, you'll have little
trouble quickly becoming an expert in that language as well.
There's almost no limit to the way in which you can customise applications built using products embedded with
VBA. And as a bonus, a growing number of non Microsoft Windows based packages are also now shipping with VBA
included.
So give your career prospects a boost, and have some fun as well, by exploring the possibilities of
programming with VBA.
The birth of the bug
Did you know: Visual Basic programmers, like developers cutting code with any other programming language, can
unwittingly introduce errors into their programs. However, not all programmers know why these sorts of
mistakes are generically referred to as "bugs".
According to programming folklore, the birth of the bug can be traced back to an incident which occurred with
one of the first computers ever assembled. Built out of the then revolutionary electromagnetic switches, a
moth became wedged between a set of relay contacts of one of these switches, causing both the hardware, and
the program it was executing, to malfunction.
And as they say, the rest is history.
The value of using VB constants
Did you know: When writing VB code, it's always a good idea to use constants whenever you can. For
example, the following statement could be used to calculate the amount of pay for a week's work. The formula
used is a simple one - the number of hours worked in the week multiplied by the hourly rate, which, in this
case, is arbitrarily set at thirty dollars an hour.
Pay = NoOfHours * 30
However, a better way of writing the code is to use a constant for the hourly rate. The first step is to
declare an appropriately named constant using the following statement.
Const HourlyRate = 30
And then to rewrite the original statement as follows:
Pay = NoOfHours * HourlyRate
It may seem a trivial change, but the use of constants like this in VB programs offers three significant
advantages:
- Constants make it easier to follow what the programmer is attempting to do.
The number 30 by itself, as used in the original statement, conveys little meaning. However, having a
constant explicitly named HourlyRate leaves no room whatsoever for confusion. And when you're writing complex
programs where lots of different values or text strings are needed, constants make programs easier to read,
understand, and debug. Not only do constants make it easier for other people to read programs, you yourself
will also appreciate their use when you have to modify your own programs months, or even years, later
- Constants can cut down the amount of memory a program needs to run
successfully
- Modifying programs that use constants is a lot easier than changing
programs which have explicit numbers or text strings sprinkled throughout the code. With a constant, you
simply change the value in one place in the program, and that's it. The coding part of the job is complete.
However, some programmers put forward the false argument that, with today's sophisticated editors, you can
just as easily change multiple occurrences of a literal number or text string with just a few keystrokes
anyway. So why go to the extra bother of declaring constants? But the real danger with that approach is
you're more likely to incorrectly alter the code through an oversight. This is particularly the case where a
number such as 30 could easily be used in different places in the program for completely different
purposes.
VB graphics
Did you know: Because Windows programs developed with VB can be easily enhanced with graphics and
pictures, it's worth knowing some of the differences and uses of the various picture file formats
available.
One of the most familiar is the bitmap file format which, by convention, is a file name with a .BMP
extension.
The graphic within a bitmap is stored as a collection of pixels, with each individual pixel represented by a
bit. (A pixel, which is shorthand for picture element, is the smallest amount of a screen's real estate which
can be used to visually convey information. The number of pixels, measured both horizontally and vertically,
available on a particular screen, depends on the type of hardware being used, and also the associated
resolution setting. Common settings for the resolution are 640 pixels horizontally and 480 vertically, 1280
and 1024 respectively, and so on.)
Icons are special bitmaps, with an extension of .ICO. Their smallness, only 32 pixels vertically and 32
horizontally, make them the ideal choice for VB buttons, or for the minimised windows used to denote VB
applications which, whilst still running, are not actually currently being used.
Another common picture format is the metafile, with an extension of .WMF. The images stored within this
particular type of file are represented using a set of graphical objects such as lines and squares - there is
no direct reference to pixels. One of the advantages with metafiles is they usually consume less space than
equivalent bitmap representations.
Finally, GIF and JPG are examples of two highly popular, and standard, file formats which VB applications can
take advantage of when manipulating images.
Reprinted from the April 1998 issue of PC Update, the
magazine of Melbourne PC User Group, Australia
|