Thursday, December 25, 2008

Erl - A unique feature in Visual Basic 6

Erl is probably one of the least known features in VB6. There is a very strong possibility that you have never even heard its name. I stumbled upon it a few months back while trying to get something on error handling mechanisms in VB6.


Remember a unique feature about the old procedural languages like C++, BASIC or PASCAL? They provided a very important statistic about the error that occurred in the program. Apart from the error number and description, they provided the line number on which the error occurred. This feature later became quite extinct in the later 4th generation languages like Foxpro. However Visual-Basic, the popular successor of the BASIC language maintained this compatibility. However, it was not very well documented or much popular.

This feature in VB6 is called Erl. In fact, Erl is a special variable name in VB6. This variable contains the line-number where the error occurred. However, there is a small price to pay in order to use this variable. It requires that we label each line number as we used to do it in old BASIC language. But that is only inside the procedure where “on error resume” statement is used. I tried this thing by creating a new Standard-Exe project. Then, added one blank form and a new command-button. Inside the button, I added the following code:

Private Sub Command1_Click()
1: On Error GoTo em
2: Dim val1 As Integer
3: Dim val2 As Integer
4: Dim result As Integer
5: val1 = 20
6: val2 = 0
7: result = val1 / val2
8: Exit Sub
em:
MsgBox "Error occured on line " & Erl & ": " & Err.Description
End Sub


As you can see, I’ve deliberately created an error on line number 7 by dividing 20 by zero. And guess what? The error message displayed contains the line number 7 when I run the program!

If you omit the labeling for each statement, Erl variable would contain zero.

I also created my own variable Erl inside the above procedure just out of curiosity. In that case, my new variable over-rides the system variable Erl and it would behave as just another ordinary variable.

Meanwhile, I’ve tried to do the same thing in VB.NET. i.e. displaying the error-number. I haven’t found a way to do so yet. MS has also removed this built-in Erl variable. If you have got any suggestions, then do shoot them.

To give me feedback or shoot any queries, use the comments link on this page.

3 comments:

  1. I recall this from early Basic but I had forgotten all about it until I was debugging recently and wishing Basic had this line number capability like the old days (1980's) :-) Excellent tip. Too bad MS removed it from .NET. Thanks.

    ReplyDelete
  2. Here's the help file for a VB add-in call MZ-Tools. It says it works in VB.NET too. (untried by me)
    http://www.mztools.com/v8/onlinehelp/index.html?add_remove_line_numbers.htm

    ReplyDelete