Thursday, 11 November, 2004

If you're ever debugging a Visual Basic application and you're using the "On Error Goto [Label]" construct then you'll love this undocumented function: erl. It tells you on which line the error occured. I'm no fan of Visual Basic 6; in fact it is a mess of a language. It doesn't know if it's a procedural or object orientated language. On the one hand it supports classes in some half-arsed way and on the other it supports horrible constructions like the goto statement and line numbers that pretty much every programmer agrees are the spawn of Satan.

In fact this trick is the only useful reason i've seen to use line numbers in Visual Basic. Here's an example of it in action:

0Sub Example()
10on error goto handleError:
20 ... do stuff.
....
90lSomeValue = clng("This is a string designed to cause an error")
100Exit sub
110handleError:
120msgbox "An error occured on line:" & erl()
130End Sub

The line number returned would be "120". Why on earth would ia rhia useful I hear you ask? Try debugging Visual Basic COM components used in a web-site. It really eases the pain of trying to locate the source of those difficult bugs where the error message given is about as good as useful as a chocolate fireguard. It's nice when every one in a while you learn an elegant new trick that makes light work of a formally frustraing job! This scrap of knowledge has made Visual Basic sit a little easier in my stomach hehe Emoticon: Smile

Simon.

23:06:17 GMT | #Programming | Permalink
XML View Previous Posts