vb.nettypescastingdirectcast

Casting nothing into value type: different behavior depending on the number of indirection


In VB, i have different behavior with DirectCast and casting into value type (double, int, ...) depending of the number of indirection

DirectCast(nothing, Double)
return 0

But, if I try to cast something like a element of a matrix equals to nothing, there is an exception

Dim pArray as Object() = { nothing, 1.5, 2.27, -3.0}
DirectCast(pArray(1), Double)  'work with no issue
DirectCast(pArray(0), Double)  'Exception : Cannot convert to double

In the same way :

Dim TestCasting as object = nothing
Directcast(TestCasting, double) 'Exception : Cannot convert to double 

How can i make so the DirectCast of pArray(0) work the same way than DirectCast(nothing, double) ?


My post was an example that highlight the issue without any concerns for the rest of the code.

To be thrill. here is an example that could pose some issue. Let's take a random table (no primary key or anything but never mind) :

TABLE [dbo].[IDENTIFICATION] (
    [USER_ID]        INT            IDENTITY (1, 1) NOT NULL,
    [PASSWORD]       NVARCHAR(50)   NULL,
    [EXPIRATION_D]   DATETIME       NOT NULL,
    [LAYOUT]         INT            NULL,
);

Now, I have a method which returns an Object(,)

Dim pArray as Object(,)  = myconnection.GetSqlRequest("Select USER_ID, PASSWORD, EXPIRATION_D, LAYOUT from IDENTIFICATION where USER_ID = 3")

this may return something like { 3, "StackOverflow", New Date(2110,01,01), nothing} because layout is an optional field.

I can do as such:

if pArray(0,3) is nothing then
   Layout = 0
Else 
   Layout = DirectCast(pArray(0,3), Double)
End if

But my goal would be to just do :

Layout = DirectCast(pArray(0,3))

mostly because I'm refactoring a huge part of a code i didn't write and also because it bugs me that DirectCast(nothing, Double) return 0 except in this case.


Solution

  • That's simple: don't use Nothing when you store Doubles in an array and don't use a Object() when you actually want to store doubles.

    Wait, it would be better to use a Double?() anyway. Nullables can be initialized with null/Nothing Then you don't need a cast at all.

    Dim pArray As Double?() = {Nothing, 1.5, 2.27, -3.0}
    Dim first = pArray(0)
    If first.HasValue Then
        ' No, it's a Nullable(Of Double)/Double? without a value
    End If
    

    Edit According to your original question. The better question would be why this works in VB:

    Dim d as Double = DirectCast(Nothing, Double) ' => 0.0
    

    The reason: Nothing in VB.Net is the equivalent of default(T) in C#: the default value for the given type which is 0 for numeric types, Date.MinValue for Date and Nothing(now in the meaning of null in C#) for reference types.

    So DirectCast(Nothing, Double) will be converted implicitely to the Double 0. Whereas the Object() contains really objects which is a placeholder for everything. But Nothing is normally a "unknown" state of any object and not a double, so the DirectCast which is very strict fails. It would also throw a runtime error if you would change the value -3.0 to -3 since that is actually an Integer.

    To cut a long story short,

    use CType instead of DirectCast for those conversions and it'll work.

    Dim obj As Object() = {Nothing, 1.0, 2}
    Dim d1 = CType(obj(0), Double) ' => 0.0
    Dim d2 = CType(obj(1), Double) ' => 1.0
    Dim d3 = CType(obj(2), Double) ' => 2.0