Refactoring code (2)-handling null strings

Source: Internet
Author: User

Recently, I found that a piece of code is not very good at processing null values. I wrote two sections of code, one for me and the other for my brother. Which one is better?

'<Summary> convert an empty field to an empty string </Summary>
'<Param name = "field"> Field instance </param>
'<Return> Field content </return>
Public Function convertnull2string (field as ADODB. Field) as Variant
Require me. isinstance (field), me, "convertnull2string", "field must be instantiated"
If field. type = advarwchar or field. type = adchar then
Convertnull2string = field. Value &""
Else
Convertnull2string = field. Value
End if
End Function

'Get the value of the specified field
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

Of course, it would be even better if it is processed in the SQL statement, as shown below:

Select a, isnull (B, '0') as B from tnulls

Related Articles:
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.