重构代码(2)-处理空字符串
最近发现一段代码在处理空值上不是很好,分别写了2段代码,一个是我的,一个是兄弟的,哪个好点呢?
'<summary>将空字段转换为空串</summary>
'<param name="Field">字段实例</param>
'<return>字段内容</return>
Public Function ConvertNull2String(Field As ADODB.Field) As Variant
Require Me.IsInstance(Field), Me, "ConvertNull2String", "Field必须实例化"
If Field.Type = adVarWChar Or Field.Type = adChar Then
ConvertNull2String = Field.Value & ""
Else
ConvertNull2String = Field.Value
End If
End Function
'取得指定字段得值内容
Public Function GetFieldValue(Field As Field, Optional IsNumeric As Boolean = False) As Variant
On Error GoTo e
If Not Field Is Nothing Then
If IsNull(Field.Value) Then
If IsNumeric Then
GetFieldValue = 0
Else
GetFieldValue = ""
End If
Else
GetFieldValue = Field.Value
End If
Else
GetFieldValue = ""
End If
Exit Function
e:
GetFieldValue = ""
End Function
'<param name="Field">字段实例</param>
'<return>字段内容</return>
Public Function ConvertNull2String(Field As ADODB.Field) As Variant
Require Me.IsInstance(Field), Me, "ConvertNull2String", "Field必须实例化"
If Field.Type = adVarWChar Or Field.Type = adChar Then
ConvertNull2String = Field.Value & ""
Else
ConvertNull2String = Field.Value
End If
End Function
'取得指定字段得值内容
Public Function GetFieldValue(Field As Field, Optional IsNumeric As Boolean = False) As Variant
On Error GoTo e
If Not Field Is Nothing Then
If IsNull(Field.Value) Then
If IsNumeric Then
GetFieldValue = 0
Else
GetFieldValue = ""
End If
Else
GetFieldValue = Field.Value
End If
Else
GetFieldValue = ""
End If
Exit Function
e:
GetFieldValue = ""
End Function
当然如果在Sql语句里边处理好了,就更棒了,向下边这样:
SELECT A, ISNULL(B, '0') AS B FROM tNulls
相关文章:
http://www.access-cn.com/Article/Class5/Class28/Class30/200508/1744.html
http://www.zdnet.com.cn/developer/database/story/0,3800066906,39412365,00.htm
http://unruledboy.cnblogs.com/archive/2004/06/27/18988.aspx