Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Turning Bounds Checking On Or Off

When you use arrays, Visual Basic checks to make sure that you don’t inadvertently try to access memory past the end or before the beginning of the array when the program runs, which is an error that could corrupt memory. In the early days of programming, however, you could use array index values that were past the end of an array without causing a compiler error, and some programmers used to rely on that to create some programming tricks involving accessing memory far beyond what they were supposed to stick with (especially in C, where the name of an array is really a pointer). That practice is heavily discouraged today, but some programmers must still have a soft spot for it, because Visual Basic allows you to turn off array bounds checking. (In fairness, there are one or two other reasons you might want to turn off bounds checking, such as not having the program halt for bounds violations while you’re trying to track down a bug or, conceivably, for performance reasons.)

What does a bounds violation look like? Here’s an example in code where we set up an array and then try to access a location past the end of it:

Private Sub Command1_Click()
    Dim Addresses(1 To 10) As Integer
    Addresses(1) = 1                    'Fine
    Addresses(11) = 11                  'Problem!
End Sub

If you were to run this code, you’d get the error box shown in Figure 2.23—unless you turn off bounds checking.


Figure 2.23  An out-of-bounds error.

You can turn off bounds checking by following these steps:

1.  Select the Properties item in the Project menu to open the Project Properties box.
2.  Select the Compile tab in the Project Properties window.
3.  Click the Advanced Optimizations button in the Project Properties window to open the Advanced Optimizations box, as shown in Figure 2.24.


Figure 2.24  Turning off bounds checking.

4.  Select the Remove Array Bounds Checks checkbox to turn off array bounds checking.

That’s it—now you’ve turned off array bounds checking.


WARNING!  Before turning off array bounds checking, however, make sure you have a really good reason for doing so; you may find your program crashing Windows as it makes illegal use of memory.

Checking For Pentium Errors

Some time ago, one version of the Intel Pentium suffered from a well-publicized hardware bug in the floating point instruction named FDIV. Intel responded quickly and offered to replace the defective chips, but it’s reasonable to expect some are still out there.

For that reason, Visual Basic has a check to make sure the Pentium your program runs on is safe. That check is enabled by default, but if for some reason you want to turn it off (although it is hard to see why you would), you can turn off the Pentium FDIV check with these steps:

1.  Select the Properties item in the Project menu to open the Project Properties box.
2.  Select the Compile tab in the Project Properties window.
3.  Click the Advanced Optimizations button in the Project Properties window to open the Advanced Optimizations box (as shown earlier in Figure 2.24).
4.  Select the Remove Safe Pentium FDIV Checks checkbox.

That’s it—you’ve disabled the FDIV Pentium check. Although you might want to do this yourself if you know what you’re doing, it’s not recommended that you do this in any software you release commercially.

Managing Add-Ins

The deadline for your project is fast approaching, and the pressure is on. Suddenly it occurs to you that you’ve already written a lot of the components you need to use—the day is saved! But how can you access those components? One easy way is to use the Visual Component Manager. But when you check the Visual Basic Add-Ins menu, you don’t see the Visual Component Manager there. How do you add it?

You use the Visual Basic Add-In Manager to add this—and any other—add-in. Here’s how to use the Add-In Manager:

1.  Select the Add-In Manager item in the Visual Basic Add-In menu.
2.  The Add-In Manager opens, as shown in Figure 2.25.


Figure 2.25  The Visual Basic Add-In Manager.

3.  Select the add-ins you want, as also shown in Figure 2.25, and close the Add-In Manager.

That’s it—now you’ve added the add-in you want. To remove it, simply deselect the add-in’s box in the Add-In Manager. (Some add-ins have an annoying habit of starting when Visual Basic starts, grinding on for a long time while it loads and taking up a lot of memory, which can be annoying if you don’t need the add-in any more.)

Adding ActiveX Controls And Insertable Objects To Projects

Been away from Visual Basic for a while and need to get back into the swing of things? You’ve been designing your project but suddenly realize you need a Microsoft Grid control. That’s an ActiveX control—how do you add those again? Use the Add File To Project menu item? Double-click the toolbox and hope an Insert dialog box comes up? Add a reference to the actual Grid control’s OCX file, asctrls.ocx, to the project?

None of those—here’s how you do it:

1.  Select the Project menu’s Components item.
2.  The Visual Basic Components box opens, as shown in Figure 2.26; click the Controls tab in the Components dialog box.


Figure 2.26  The Visual Basic Components dialog box.

3.  Select the ActiveX control you want to add in the Components box, then close the Components box. The new control will appear in the toolbox.


TIP:  If the ActiveX control you want to add to a Visual Basic project doesn’t appear in the Components dialog box, it may not have been registered with Windows properly. Try using the regsvr32.exe tool in the Windows\system directory to register it again.

You can also add insertable objects like Microsoft Word or Microsoft Excel objects to a Visual Basic project by using the Components dialog box. Instead of the Controls tab in the Components box, however, you use the Insertable Objects tab and select the object you want; that object will appear in the toolbox, and you can use it in your project from then on. For example, we’ve inserted an Excel worksheet into the Visual Basic project in Figure 2.27.


Figure 2.27  A Microsoft Excel worksheet in a Visual Basic project.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.