Visual Basic Tips #45


----------------------------------------------

TipWorld - http://www.tipworld.com
The Internet's #1 Source for Computer Tips, News, and Gossip

Proudly presents:
Visual Basic

----------------------------------------------


*1. WHY A VARIABLE'S DATA TYPE MATTERS

If you don't declare a variable's data type, Visual Basic will 
do so for you and assign the Variant data type to your variable.
Now, you may not care, or you may not think it matters--and 
you'd be right, to a degree. However, assigning the most 
appropriate data type to your variable gives you control over 
that variable. What do we mean by control?

First, a data type restricts a variable to certain types of 
data. If you're storing values to use in a later calculation, 
you certainly wouldn't want VB to accept a string value, for 
example. In this case, you'd declare your variable as one of the
many value data types, such as Byte, Integer, Long Integer, 
Single, Double, and so on. When you do so, VB will return an 
error if you attempt to enter a string value instead of an 
appropriate number value. This quality extends to dates as 
well--if you declare a variable as a date, it won't accept 
anything other than a date. So, declaring the appropriate data 
type is one (very easy) way to validate data.

Second, try to assign the data type that meets the needs of your
variable but uses the least amount of resources, because each 
data type requires a certain number of bytes--and of course, the
fewer you expend, the more efficient your application. A Variant
data type requires 16 bytes while an Integer data type requires 
only 2. If the Integer data type is adequate but you use the 
Variant data type, you've wasted 14 bytes. This may not seem 
like a big deal, but if your application is large and your 
resources are limited, these unnecessary expenditures can add up
quickly and affect the performance of your application.

Don't go overboard and be too stingy about your declarations. 
After all, sometimes more is better. For instance, if you're 
using a function to store a value between 0 and 255, you might 
want to use the Byte data type--but only if you are first sure 
you won't ever need to store a negative value. Also, consider 
the way your function will use these Byte values. If the result 
of calculating a Byte value might return a value other than one 
that falls between 0 and 255, you can't use the Byte data type 
to store the results just because the Byte data type was 
appropriate for the numeric elements you used 
in the calculation.


*2. AVOIDING GOTO

Visual Basic's GoTo statement allows you to control the flow 
of your code. However, you'll want to use this statement as 
little as possible. In fact, GoTo is easily the black sheep of
VB statements, since some developers refuse to use the 
statement except when referring to an error handler.

Now, we're not trying to pass judgment on those who use the 
GoTo statement. If you use it with no problems, then continue 
with what works. However, you need to be aware that this 
statement can cause problems. First, it can be difficult to 
debug a module that jumps around a lot. Second, the statement 
requires no conditions; it simply jumps without looking first, 
so there's no control.

If you're using GoTo a lot, you might want to review some of 
the conditional structures such as If...Then, Select Case, 
Do...Loop, and so on. Then, try to replace some of your GoTo 
statements with one of these more stable statements.


*3. REPEAT i IN NEXT LOOP

When using the For statement, you can omit specifying the 
loop's variable name in the Next statement. For instance,

For i = 1 To 10 
... 
Next

is a legitimate loop. However, the code

For I = 1 To 10 
... 
Next i

is more obvious--it is easy to see where the For loop ends. 
This may not seem important in such a simple example, but in a 
complex loop with many lines of code, it can make spotting the 
end much easier.

If you want the loop to be even more meaningful, use a 
descriptive name--instead of I--for the looping variable. 
Simply name the variable as you would any other--by describing 
its purpose or origin.


*4. IF...THEN VERSUS IIF()

If you routinely assign the result of an Iif() statement to a 
variable, you should consider replacing those statements with 
an If...Then construct. Why? First, the Iif() statement is a 
little harder to decipher because of its one line structure. 
Second, the If...Then construct is faster than the Iif() 
statement. That's right--an If...Then is more than twice as 
fast as an Iif() statement that performs the same task.


*5. FIXED-LENGTH VERSUS VARIABLE-LENGTH

We're often asked what the difference is between a fixed-length 
string and a variable-length string. The difference is just as 
the name suggests. A fixed-length string is fixed in 
size--which means it can handle only so many characters and no 
more. For instance, the statement

Dim strFixed As String * 10

limits strFixed to ten characters or fewer. 
On the other hand, the statement

Dim strVariable As String

allows strVariable to grow (or shrink) as necessary.


*6. DECLARING DLL FUNCTIONS

If you're new to Visual Basic, you may also be new to working 
with DLLs. There are three main types of API libraries: GDI, 
User, and Kernel. GDI functions are related to line, text, and 
bitmap output. User functions cover messaging, dialog boxes, 
and controls. The last library, Kernel, applies to 
system-related information, such as memory allocation. 
Each library contains many functions.

Once you've located the appropriate function in one of the 
libraries, you must declare that function. In doing so, you'll 
need to know the name of the function, the number of arguments 
the function expects, their data types, how the function will 
return the result, and the result's data type. The general 
syntax for declaring an API function is

Declare Function functionname Lib "libraryname" [Alias aliasname]
[(argument list)] As resultdatatype 

It's also important to note that 32-bit calls are 
case-sensitive.


*7. SHORTCUT KEYS FOR THE IMMEDIATE WINDOW

There are several shortcut keys you can use in the Immediate 
window. Some, such as pressing the Enter key, you already know. 
(Pressing Enter runs a line of code.) In addition, Ctrl-C 
copies selected text to the Clipboard, and Ctrl-V pastes the 
contents of the Clipboard into the Immediate window. You can 
use Ctrl-X to cut the selected text (to the Clipboard). More 
useful, but less obvious, shortcut keys include the following: 

- Ctrl-L displays the Call Stack dialog box. 
- Ctrl-Enter inserts a carriage return. 
- Ctrl-Home moves the cursor to the top of the window. 
- F2 displays the Object Browser. 
- F5 continues to run an application. 
- Shift-F5 restarts an application. 
- Alt-F5 runs the error handler code or returns the error. 
- F6 switches between the Immediate and Watch windows. 
- F8 initiates Single Step mode (executes code one line
  at a time). 
- Shift-F10 displays the shortcut menu.


*8. WORKING AROUND VERSION NAME CONFLICTS

You can't use restricted keywords as the name of a form or 
control. Well, you can, but it isn't advisable. For those rare 
occasions when you do, you must first enclose the name in a 
set of square brackets in order for Visual Basic to 
accept that name.

Although we don't recommend you use this workaround in your 
normal coding, this tip does have its place. Sometimes you'll 
find that an accepted name in an earlier version is in conflict 
with a newer version because that name is now a reserved word. 
When this happens, simply enclose the name in square brackets, 
and the new version will accept and use the name.


*9. WORKING WITH TRUE-FALSE DATA

If you're new to Visual Basic, you might not realize that you 
can assign a number, or even a string, to a Boolean variable. 
Let's take a look at a quick and easy example:

Private Sub Form_Click() 
Dim bytOne As Byte 
Dim intTwo As Integer 
                      
Print bytOne = True 
Print intTwo = True 
Print bytOne = intTwo

End Sub

In this case, the True Boolean value is assigned to both a 
Byte and Integer variable, which is quite legal.

Furthermore, you might expect this short exercise to print 
True, True, and True, since if bytOne equals True and intTwo 
equals True, then bytOne must equal intTwo, right? Well, they 
don't. That's because bytOne and intTwo are different data 
types, so they aren't equal in the eyes of VB, even if you 
assign the same value to both.


*10. THE FOR...LOOP VARIABLE

The For Each...Next loop provides a convenient method for 
looping through each member of a collection or a normal array. 
Before the For Each...Next loop, the process was a bit more 
complex. Just remember this one detail--you must reference each 
element as an Object or Variant variable when working with 
collections. If you're looping through an array, however, 
you must use a Variant.
