
Recently I was asked to update our homegrown timesheet application that had been written years ago in classic ASP. The update was simply to take a user name in the string format ‘Last Name, First Name’ and convert this to our company standard email format of FirstLetterofFirstName + LastName + @companyname.com
Now it has been years since I wrote any classic ASP, so it took a little bit of looking into the various functions to refresh my memory.
I made use of the following Classic ASP functions:
- On Error Resume Next – to imitate standard try – catch block behavior
- The Split(BaseString, Separator) function to separate the name string
- The Left( function to get the first letter of the first name from the string array
- The UBound( function to get the last entry in the name array
- The Replace(BaseString, StringToBeReplaced, ReplacementString)
- The LCase( function. Alternately I remember also finding the UCase function to be quite useful on occasion.
Here is a test version of the code snippet I ended up using:
Dim UserName Dim SplitUserNameArray Dim EmailUserName UserName = "Cooney, Justin" On Error Resume Next SplitUserNameArray = Split(UserName, " ") EmailUserName = Left(SplitUserNameArray(Ubound(SplitUserNameArray)), 1) & SplitUserNameArray(0) EmailUserName = Replace(EmailUserName, "'" , "") EmailUserName = LCase(Replace(EmailUserName, "," , "")) If Err.Number <> 0 Then EmailUserName = "Unknown" End If On Error Goto 0 Response.Write(EmailUserName & "@companyname.com")
In writing this function I was especially interested in getting the error handling to work like a try-catch block, which I was successfully able to achieve. Here is the structure with the extra code removed:
On Error Resume Next ' INSERT LOGIC CODE HERE If Err.Number <> 0 Then ' INSERT CATCH CODE HERE End If On Error Goto 0
The On Error Goto 0 Command works like a charm in resetting the error and allowing continued execution of the code.
Although not much work is done these days in Classic ASP, I found this task to be fun, and I enjoyed being reminded of the ‘good old days’ when I would work with ASP, Visual Interdev (or even notepad), SQL Server 7 on a daily basis.
Classic ASP is appealing and interesting (but not fascinating) when you have to do it at work! Glad that I am not the only one. Thanks for sharing.