Reputation: 27350
How can I improve this code? What has made this long winded is the fact that I can't use string.IsNullOrEmpty on a data row and instead I have to use a dr.IsHOUSENUMBERNull method AND the string.IsNullOrEmpty to check if it is empty. Why is this? The column in the database is sometimes empty and sometimes NULL.
I'm sure this can be written better:
If Not dr.IsHOUSENUMBERNull Then
If Not String.IsNullOrEmpty(dr.HOUSENUMBER) Then
sbAddress.AppendLine(dr.HOUSENUMBER + " " + dr.ADDRESS1)
Else
sbAddress.AppendLine(dr.ADDRESS1)
End If
Else
sbAddress.AppendLine(dr.ADDRESS1)
End If
Upvotes: 1
Views: 3532
Reputation: 630509
You could do this, a bit shorter:
If dr.IsHOUSENUMBERNull OrElse String.IsNullOrEmpty(dr.HOUSENUMBER) Then
sbAddress.AppendLine(dr.ADDRESS1)
Else
sbAddress.AppendLine(dr.HOUSENUMBER + " " + dr.ADDRESS1)
End If
Or if you want it more terse, though less readable in this case I think, use If()
:
sb.Address.AppendLine(If(r.IsHOUSENUMBERNull OrElse String.IsNullOrEmpty(dr.HOUSENUMBER), dr.ADDRESS1, dr.HOUSENUMBER + " " + dr.ADDRESS1))
Upvotes: 3