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 7831707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:50:57+00:00 2026-06-02T11:50:57+00:00

Possible Duplicate: Bug?? If you assign a value to a nullable integer via a

  • 0

Possible Duplicate:
Bug?? If you assign a value to a nullable integer via a ternary operator, it can't become null

While this question may seem like a duplicate of many, it is actually being asked for a specific reason. Take this code, for example:

Dim n As Integer? = If(True, Nothing, 1)

In that code, the ternary expression should be returning Nothing, but it’s setting n to 0. If this were C#, I could say default(int?) and it would work perfectly. Now it looks like I am going to have to ditch the ternary and use a regular If block, but I really want to use the ternary.

If Nothing were truly VB.NET’s equivalent to C#’s default, how can you explain this behavior?

  • 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-02T11:50:59+00:00Added an answer on June 2, 2026 at 11:50 am

    The VB.NET equivalent to C#’s default is the keyword Nothing. The code you wrote should compile just fine so long as Id.Value returns an Integer value.

    The reason your updated sample is going wrong is because of the nature of Nothing. In VB.NET Nothing is the empty value and it’s convertible to any type. Now for an If expression, the compiler has to infer what the type of the return should be, and it does this by looking at the two value arguments.

    The value Nothing has no type, but the literal 1 has the type Integer. Nothing is convertible to Integer so the compiler determines Integer is the best type here. This means when Nothing is chosen as the value, it will be interpreted as Integer and not Integer?.

    The easiest way to fix this is to explicitly tell the compiler that you want 1 to be treated as an Integer?.

    Dim n As Integer? = If(True, Nothing, CType(1, Integer?))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: php == vs === operator Reference - What does this symbol mean
Possible Duplicate: Floating point inaccuracy examples PHP rounding issue - Is this a bug?
Possible Duplicate: Bug with starting Instruments via Profile under XCode 4.1 Build 4B110 Instruments
Possible Duplicate: How can I get this ASP.NET MVC SelectList to work? What the
Possible Duplicate: Workarounds for JavaScript parseInt octal bug well i have this http://jsfiddle.net/gMDfk/1/ The
Possible Duplicate: Is it a bug that Microsoft VS C++ compiler can Initialize a
Possible Duplicate: Safari: Absolutely positioned DIVs not moving when updated via DOM This answer
Possible Duplicate: C compiler bug (floating point arithmetic)? I've got two doubles which I
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: Workarounds for JavaScript parseInt octal bug I'm parsing a string to check

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.