1. Home
  2. Computing & Technology
  3. Visual Basic

Chapter 10 - Using Modules and Procedures - Part 2

By Dan Mabbutt, About.com

5 of 7

Assignment from the previous lesson - Looping with Try ... Catch

In the previous lesson about Structured Error Handling, I gave you a challenge to write the example that the book promised and didn't deliver: Recode the Disk Drive Error Handler program to allow ten retries before giving up.

Here's the example from the book that needs to be recoded:

~~~~~~~~~~~~~~~~~~~~~~~~~
Try
  PictureBox1.Image = _
    System.Drawing.Bitmap.FromFile("a:\fileopen.bmp")
Catch
  MsgBox("Insert the disk in drive A, then click OK!")
  Try
    PictureBox1.Image = _
      System.Drawing.Bitmap.FromFile("a:\fileopen.bmp")
  Catch
    MsgBox("File Load feature disabled")
    Button1.Enabled = False
  End Try
End Try
~~~~~~~~~~~~~~~~~~~~~~~~~

Like just about every problem, this one can be programmed in an almost infinite number of ways ... and a lot of them don't even have to involve a module or procedure. So if your solution is different than mine ... Hey! Your's might be a vastly superior solution! The test is whether it really does solve the problem.

But since the goal was to use some kind of procedure, my solution does. And, as is my habit, I have thrown in a few extra features too. My solution uses a Function call to check the disk and the location of the file is a string that is passed to the Function.

5 of 7

Explore Visual Basic

More from About.com

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Using Modules and Procedures - Part 2

©2008 About.com, a part of The New York Times Company.

All rights reserved.