Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9083345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:45:12+00:00 2026-06-16T20:45:12+00:00

In VB, i have different behavior with DirectCast and casting into value type (

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T20:45:13+00:00Added an answer on June 16, 2026 at 8:45 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have different behavior in a python script, depending on the type
I was wondering if it is possible to have different behavior depending on a
I want to write a console application that have a different behavior depending if
I am looking for a clean way to have different behavior depending on whether
i have used fileupload control in my webpage but it show different behavior in
I have few form which have different behavior in 6i version and 10g version.
all. I have different behavior of function urldecode() in PHP 5.2.x . Especially you
Both firefox's js engine/gjs (spidermonkey) and webkit's jscore seems to have different behavior when
I have the following situation where a client class executes different behavior based on
Please look at this fiddle . I have different behavior for following two classes.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.