{"id":539,"date":"2022-08-30T15:03:02","date_gmt":"2022-08-30T15:03:02","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/casting-nothing-into-value-type-different-behaviour-depending-of-the-number-of-indirection-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:03:02","modified_gmt":"2022-08-30T15:03:02","slug":"casting-nothing-into-value-type-different-behaviour-depending-of-the-number-of-indirection-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/casting-nothing-into-value-type-different-behaviour-depending-of-the-number-of-indirection-collection-of-common-programming-errors\/","title":{"rendered":"Casting nothing into value type : different behaviour depending of the number of indirection-Collection of common programming errors"},"content":{"rendered":"<p>In VB, i have different behaviour with DirectCast and casting into value type (double, int, &#8230;) depending of the number of indirection<\/p>\n<pre><code>DirectCast(nothing, Double)\n<\/code><\/pre>\n<p>return 0<\/p>\n<p>But, if i try to cast something like a element of a matrix equals to nothing, there is an exception<\/p>\n<pre><code>Dim pArray as Object() = { nothing, 1.5, 2.27, -3.0}\nDirectCast(pArray(1), Double)  'work with no issue\nDirectCast(pArray(0), Double)  'Exception : Cannot convert to double\n<\/code><\/pre>\n<p>In the same way :<\/p>\n<pre><code>Dim TestCasting as object = nothing\nDirectcast(TestCasting, double) 'Exception : Cannot convert to double \n<\/code><\/pre>\n<p>How can i make so the directcast of pArray(0) work the same way than DirectCast(nothing, double) ?<\/p>\n<p>EDIT : My post was an example that highlight the issue without any concerns for the rest of the code.<\/p>\n<p>To be thrill. here is an exemple that could pose some issue. Let&#8217;s take a random table (no primary key or anything but nevermind) :<\/p>\n<pre><code>TABLE [dbo].[IDENTIFICATION] (\n    [USER_ID]        INT            IDENTITY (1, 1) NOT NULL,\n    [PASSWORD]       NVARCHAR(50)   NULL,\n    [EXPIRATION_D]   DATETIME       NOT NULL,\n    [LAYOUT]         INT            NULL,\n);\n<\/code><\/pre>\n<p>Now, i have a method who return a Objec(,)<\/p>\n<pre><code>Dim pArray as Object(,)  = myconnection.GetSqlRequest(\"Select USER_ID, PASSWORD, EXPIRATION_D, LAYOUT from IDENTIFICATION where USER_ID = 3\")\n<\/code><\/pre>\n<p>this may return something like { 3, &#8220;StackOverflow&#8221;, New Date(2110,01,01), nothing} because layout is an optional field.<\/p>\n<p>I can do as such :<\/p>\n<pre><code>if pArray(0,3) is nothing then\n   Layout = 0\nElse \n   Layout = DirectCast(pArray(0,3), Double)\nEnd if\n<\/code><\/pre>\n<p>But my goal would be to just do :<\/p>\n<pre><code>Layout = DirectCast(pArray(0,3))\n<\/code><\/pre>\n<p>mostly because i&#8217;m refactoring a huge part of a code i didn&#8217;t write and also because it bugs me that DirectCast(nothing, Double) return 0 except in this case.<\/p>\n<ol>\n<li>\n<p>That&#8217;s simple: don&#8217;t use <code>Nothing<\/code> when you store <code>Doubles<\/code> in an array and don&#8217;t use a <code>Object()<\/code> when you actually want to store doubles.<\/p>\n<p>Wait, it would be better to use a <code>Double?()<\/code> anyway. Nullables can be initialized with <code>null\/Nothing<\/code> Then you don&#8217;t need a cast at all.<\/p>\n<pre><code>Dim pArray As Double?() = {Nothing, 1.5, 2.27, -3.0}\nDim first = pArray(0)\nIf first.HasValue Then\n    ' No, it's a Nullable(Of Double)\/Double? without a value\nEnd If\n<\/code><\/pre>\n<p><strong>Edit<\/strong> According to your original question. The better question would be why this works in VB:<\/p>\n<pre><code>Dim d as Double = DirectCast(Nothing, Double) ' =&gt; 0.0\n<\/code><\/pre>\n<p>The reason: <code>Nothing<\/code> in VB.Net is the equivalent of <code>default(T)<\/code> in C#: the default value for the given type which is 0 for numeric types, <code>Date.MinValue<\/code> for <code>Date<\/code> and <code>Nothing<\/code>(now in the meaning of <code>null<\/code> in C#) for reference types.<\/p>\n<p>So <code>DirectCast(Nothing, Double)<\/code> will be converted implicitely to the <code>Double<\/code> 0. Whereas the <code>Object()<\/code> contains really objects which is a placeholder for everything. But <code>Nothing<\/code> is normally a &#8220;unknown&#8221; state of any object and not a double, so the <code>DirectCast<\/code> which is very strict fails. It would also throw a runtime error if you would change the value <code>-3.0<\/code> to <code>-3<\/code> since that is actually an <code>Integer<\/code>.<\/p>\n<p>To cut a long story short,<\/p>\n<p><strong>use <code>CType<\/code> instead of <code>DirectCast<\/code> for those conversions and it&#8217;ll work.<\/strong><\/p>\n<pre><code>Dim obj As Object() = {Nothing, 1.0, 2}\nDim d1 = CType(obj(0), Double) ' =&gt; 0.0\nDim d2 = CType(obj(1), Double) ' =&gt; 1.0\nDim d3 = CType(obj(2), Double) ' =&gt; 2.0\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 20:29:46. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>In VB, i have different behaviour with DirectCast and casting into value type (double, int, &#8230;) 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, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-539","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/539","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=539"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/539\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}