.Net Code Monkey RSS 2.0
 Friday, January 06, 2012
Operators for DBNull.Value, continues from Part 2

In this article I shall deal with operators, specifically the ones which will allow assignment of a value to the DateTimeNull object or vice-versa. For example I want to be able to run this code:
Dim nullableDate As DateTimeNull = Nothing
nullableDate = DBNull.Value
To do this I need to create a couple of shared (non-instance) 'Widening Operator' for DBNull which will allow the compiler to cast the values.
#Region "Operators for DBNull"

    ''' <summary>
    ''' Converts a System.DBNull object to a DateTimeNull object.
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns>Returns a DateTimeNull with a Null value.</returns>
    ''' <remarks></remarks>
    Public Shared Widening Operator CType(ByVal value As System.DBNull) As DateTimeNull
        Return New DateTimeNull(value)
    End Operator

    ''' <summary>
    ''' Converts a DateTimeNull object to a System.DBNull object.
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <exception cref="InvalidCastException">Thrown if the DateTimeNull is not null.</exception>
    Public Shared Widening Operator CType(ByVal value As DateTimeNull) As System.DBNull
        If value.IsNull Then
            Return System.DBNull.Value
        End If
        Throw New InvalidCastException("A non null DateTimeNull cannot be cast to DBNull.")
    End Operator

#End Region
The first operator will return a DBNull.Value to be asigned to the DateTimeNull object, like so
nullableDate = DBNull.Value
The second widening operator is required when we want to do some comparisions of the DateTimeNull to DBNull.Value. To see what I mean, we first need to add an equality operator.
    ''' <summary>
    ''' Performs a logical comparison between a DateTimeNull object 
    ''' and a System.DBNull object to determine whether they are equal.
    ''' </summary>
    ''' <param name="d1">A DateTimeNull object.</param>
    ''' <param name="d2">A System.DBNull object.</param>
    ''' <returns>Returns <c>true</c> if d1 is null.</returns>
    Public Shared Operator =(ByVal d1 As DateTimeNull, ByVal d2 As System.DBNull) As Boolean
        Return d1.IsNull
    End Operator
This will allow us to as this question...
    If (nullableDate = DBNull.Value) Then
        ' Do something...
    End If
But without the second Widening operator, we can not ask this question, without the compiler complaining...
    If (DBNull.Value = nullableDate) Then
        ' Do something...
    End If
Try it and see. If you comment out the second widening operator, then the equality test with DBNull.Value as the left hand value will not compile. Uncomment it and all is well!

Finaly, we'll add in a not equal operator.
    ''' <summary>
    ''' Performs a logical comparison between a DateTimeNull object 
    ''' and a System.DBNull object to determine whether they are not equal.
    ''' </summary>
    ''' <param name="d1">A DateTimeNull object.</param>
    ''' <param name="d2">A System.DBNull object.</param>
    ''' <returns>Returns <c>true</c> if d1 is not null.</returns>
    Public Shared Operator <>(ByVal d1 As DateTimeNull, ByVal d2 As System.DBNull) As Boolean
        Return Not d1.IsNull
    End Operator
In the next artical we'll cover using widening operators for casting to and from System.Datetime and SqlDateTime. (TBC)...



Friday, January 06, 2012 1:20:15 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.Net | Asp.Net | Casting | VB.Net | Widening Operators
Archive
<September 2014>
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
Blogroll
 Clemens Vasters
 Harry Pierson
Passion * Technology * Ruthless Competence
 Joshua Flanagan
A .NET Software Developer
 Michael Schwarz's Blog
Developing applications on the Microsoft platform since Windows 3.1!
 Omar Shahine
Yet another Microsoft blogger
 Scot GU
Scott Guthrie lives in Seattle and builds a few products for Microsoft
 Scott Hanselman
Programming Life and the Zen of Computers
 Tom Mertens
Tom's corner
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2014
Duane Wingett
Sign In
Statistics
Total Posts: 52
This Year: 9
This Month: 0
This Week: 0
Comments: 49
Themes
Pick a theme:
All Content © 2014, Duane Wingett
DasBlog theme 'Business' created by Christoph De Baene (delarou)