Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to tell difference for 0.0 and Nothing for variable in vb.net 2005

How to tell difference for 0.0 and Nothing for variable in vb.net 2005

Scheduled Pinned Locked Moved Visual Basic
questioncsharphelptutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andraw Tang
    wrote on last edited by
    #1

    Hi, dear all, I create a variable as following Dim xx as double at now, it will return true if I check as following since I didn't initialize it. if xx = Nothing then ... end if 'RETURN TRUE But the problem is that after I initialize it to cdbl(0), it still return true while check Nothing. xx = cdbl(0) if xx = Nothing then ...end if ''''STILL RETURN TRUE so how can I tell the difference for a variable initialized or not? Thanks!

    I D 2 Replies Last reply
    0
    • A Andraw Tang

      Hi, dear all, I create a variable as following Dim xx as double at now, it will return true if I check as following since I didn't initialize it. if xx = Nothing then ... end if 'RETURN TRUE But the problem is that after I initialize it to cdbl(0), it still return true while check Nothing. xx = cdbl(0) if xx = Nothing then ...end if ''''STILL RETURN TRUE so how can I tell the difference for a variable initialized or not? Thanks!

      I Offline
      I Offline
      Ian Shlasko
      wrote on last edited by
      #2

      You can't. Not for a simple type. They're automatically initialized to zero as soon as you create them, so for a double, Cbl(0) and Nothing are the same thing. If you're looking for an error code, or a way to ensure that it receives a value, you could just initialize it to double.NaN (NaN = Not a Number, the same result you get when dividing by zero). Then just test whether double.IsNaN(xx)

      Proud to have finally moved to the A-Ark. Which one are you in?
      Author of the Guardians Saga (Sci-Fi/Fantasy novels)

      A 1 Reply Last reply
      0
      • I Ian Shlasko

        You can't. Not for a simple type. They're automatically initialized to zero as soon as you create them, so for a double, Cbl(0) and Nothing are the same thing. If you're looking for an error code, or a way to ensure that it receives a value, you could just initialize it to double.NaN (NaN = Not a Number, the same result you get when dividing by zero). Then just test whether double.IsNaN(xx)

        Proud to have finally moved to the A-Ark. Which one are you in?
        Author of the Guardians Saga (Sci-Fi/Fantasy novels)

        A Offline
        A Offline
        Andraw Tang
        wrote on last edited by
        #3

        Ian, Thank you very much, I will try it.

        1 Reply Last reply
        0
        • A Andraw Tang

          Hi, dear all, I create a variable as following Dim xx as double at now, it will return true if I check as following since I didn't initialize it. if xx = Nothing then ... end if 'RETURN TRUE But the problem is that after I initialize it to cdbl(0), it still return true while check Nothing. xx = cdbl(0) if xx = Nothing then ...end if ''''STILL RETURN TRUE so how can I tell the difference for a variable initialized or not? Thanks!

          D Offline
          D Offline
          Dave Doknjas
          wrote on last edited by
          #4

          This is what nullable value types are commonly used for: Dim xx As Double? or: Dim xx As Nullable(Of Double) and then check if it's been assigned: Dim isAssigned As Boolean = xx.HasValue

          David Anton Convert between VB, C#, C++, & Java www.tangiblesoftwaresolutions.com

          A 1 Reply Last reply
          0
          • D Dave Doknjas

            This is what nullable value types are commonly used for: Dim xx As Double? or: Dim xx As Nullable(Of Double) and then check if it's been assigned: Dim isAssigned As Boolean = xx.HasValue

            David Anton Convert between VB, C#, C++, & Java www.tangiblesoftwaresolutions.com

            A Offline
            A Offline
            Andraw Tang
            wrote on last edited by
            #5

            Thanks, David, it's very useful.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups