Casting nothing into value type : different behaviour depending of the number of indirection-Collection of common programming errors
In VB, i have different behaviour 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) ?
EDIT : My post was an example that highlight the issue without any concerns for the rest of the code.
To be thrill. here is an exemple that could pose some issue. Let’s take a random table (no primary key or anything but nevermind) :
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 who return a Objec(,)
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.
-
That’s simple: don’t use
Nothing
when you storeDoubles
in an array and don’t use aObject()
when you actually want to store doubles.Wait, it would be better to use a
Double?()
anyway. Nullables can be initialized withnull/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 ofdefault(T)
in C#: the default value for the given type which is 0 for numeric types,Date.MinValue
forDate
andNothing
(now in the meaning ofnull
in C#) for reference types.So
DirectCast(Nothing, Double)
will be converted implicitely to theDouble
0. Whereas theObject()
contains really objects which is a placeholder for everything. ButNothing
is normally a “unknown” state of any object and not a double, so theDirectCast
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 anInteger
.To cut a long story short,
use
CType
instead ofDirectCast
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
Originally posted 2013-11-09 20:29:46.