Null replacement value for Reference

Source: Internet
Author: User

Imports system

Imports

System. Reflection

Namespace

Dotnetnuke

'*************************************** ******************************

'

'Null class

'

'Class for dealing with the translation of database null values

'

'*************************************** ******************************

Public class null

'Define application encoded null values

Public shared readonly property nullinteger () as integer

Get

Return-1

End get

End Property

Public shared readonly property nulldecimal () as decimal

Get

Return decimal. minvalue

End get

End Property

Public shared readonly property nulldate () as date

Get

Return date. minvalue

End get

End Property

Public shared readonly property nullstring () as string

Get

Return ""

End get

End Property

Public shared readonly property nullboolean () as Boolean

Get

Return false

End get

End Property

Public shared readonly property nullguid () as guid

Get

Return guid. Empty

End get

End Property

'Sets a field to an application encoded null value (used in presentation layer)

Public shared function setnull (byval objfield as object) as object

If not objfield is nothing then

If typeof objfield is integer then

Setnull = nullinteger

Elseif typeof objfield is single then

Setnull = nullinteger

Elseif typeof objfield is double then

Setnull = nullinteger

Elseif typeof objfield is decimal then

Setnull = nulldecimal

Elseif typeof objfield is date then

Setnull = nulldate

Elseif typeof objfield is string then

Setnull = nullstring

Elseif typeof objfield is Boolean then

Setnull = nullboolean

Elseif typeof objfield is guid then

Setnull = nullguid

Else

Throw new nullreferenceexception

End if

Else 'assume string

Setnull = nullstring

End if

End Function

'Sets a field to an application encoded null value (used in BLL layer)

Public shared function setnull (byval objpropertyinfo as propertyinfo) as object

Select case objpropertyinfo. propertytype. tostring

Case "system. int16", "system. int32", "system. int64", "system. Single", "system. Double"

Setnull = nullinteger

Case "system. Decimal"

Setnull = nulldecimal

Case "system. datetime"

Setnull = nulldate

Case "system. String", "system. Char"

Setnull = nullstring

Case "system. boolean"

Setnull = nullboolean

Case "system. guid"

Setnull = nullguid

Case else

'Enumerations default to the first entry

Dim ptype as type = objpropertyinfo. propertytype

If ptype. basetype. Equals (GetType (system. Enum) then

Dim objenumvalues as system. array = system. enum. getvalues (ptype)

Array. Sort (objenumvalues)

Setnull = system. enum. toobject (ptype, objenumvalues. getvalue (0 ))

Else

Throw new nullreferenceexception

End if

End select

End Function

'Convert an application encoded null value to a database null value (used in DAL)

Public shared function getnull (byval objfield as object, byval objdbnull as object) as object

Getnull = objfield

If objfield is nothing then

Getnull = objdbnull

Elseif typeof objfield is integer then

If convert. toint32 (objfield) = nullinteger then

Getnull = objdbnull

End if

Elseif typeof objfield is single then

If convert. tosingle (objfield) = nullinteger then

Getnull = objdbnull

End if

Elseif typeof objfield is double then

If convert. todouble (objfield) = nullinteger then

Getnull = objdbnull

End if

Elseif typeof objfield is decimal then

If convert. todecimal (objfield) = nulldecimal then

Getnull = objdbnull

End if

Elseif typeof objfield is date then

If convert. todatetime (objfield) = nulldate then

Getnull = objdbnull

End if

Elseif typeof objfield is string then

If objfield is nothing then

Getnull = objdbnull

Else

If objfield. tostring = nullstring then

Getnull = objdbnull

End if

End if

Elseif typeof objfield is Boolean then

If convert. toboolean (objfield) = nullboolean then

Getnull = objdbnull

End if

Elseif typeof objfield is guid then

If ctype (objfield, system. guid). Equals (nullguid) then

Getnull = objdbnull

End if

Else

Throw new nullreferenceexception

End if

End Function

'Checks if a field contains an application encoded Null Value

Public shared function isnull (byval objfield as object) as Boolean

If not objfield is nothing then

If typeof objfield is integer then

Isnull = objfield. Equals (nullinteger)

Elseif typeof objfield is decimal then

Isnull = objfield. Equals (nulldecimal)

Elseif typeof objfield is date then

Dim objdate as datetime = ctype (objfield, datetime)

Isnull = objdate. Date. Equals (nulldate. Date)

Elseif typeof objfield is string then

Isnull = objfield. Equals (nullstring)

Elseif typeof objfield is Boolean then

Isnull = objfield. Equals (nullboolean)

Elseif typeof objfield is guid then

Isnull = objfield. Equals (nullguid)

Else 'complex object

Isnull =

False

End if

Else

Isnull =

True

End if

End Function

End Class

End

Namespace

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.