I'm still slogging through the aforementioned legacy code, and I keep stumbling upon places where the original coder just worked waaaayyy too hard. Case in point: I step into a page load subroutine in this ASP.Net code, and there's a page and a half worth of this:
strVar1 = Request("Var1") If IsDBNull(strVar1) Then strVar1 = vbNullString strVar2 = Request("Var2") If IsDBNull(strVar2) Then strVar2 = vbNullString
There's an easier way.I'd already written some functions to handle DBNull values when pulling records out of the database:
Public Function intFromDB(ByVal dbField As Object) As Integer Try If Not (dbField Is System.DBNull.Value) Then Return Convert.ToInt32(dbField) End If Catch ex As Exception End Try Return 0 'DBNull or exception End Function Public Function strFromDB(ByVal dbField As Object) As String Try If Not (dbField Is System.DBNull.Value) Then Return Convert.ToString(dbField) End If Catch ex As Exception End Try Return "" 'DBNull or exception End Function
(There are also functions for Dates and Doubles, but you get the idea). When I use this on the lines above, I get something a little more compact:
strVar1 = strFromDB(Request("Var1")) strVar2 = strFromDB(Request("Var2"))
Update: as I work through this conversion, I've found a couple places where I could stand to have an optional param for default value.