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