Results 1 to 6 of 6

Thread: Differentiating between CDbl(0) and Nothing

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    3

    Differentiating between CDbl(0) and Nothing

    I have a conditional that takes a double variable. Basically I need the conditional to run if the variable is the value of 0.0, but not if it's nothing.

    Unfortunately it seems as though the type Double, treats a value of Nothing as 0.0

    Anyone know a way around this? Is there a wrapper double object like in Java?

    Thanks,
    Tyler

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Differentiating between CDbl(0) and Nothing

    As far as I knew you couldn't set a double to nothing. so it has to be something and defaults to 0.

    Do you mean you have an optional variable on a sub/function?

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Differentiating between CDbl(0) and Nothing

    If you have an incoming data type that can have the value of Null or Nothing (which Double cannot), then surely you can just trap on it? If the incoming data type can't take the value of Null or Nothing, then you don't have a problem...


    For example, I could use:

    Set c = Range("A:A").Find("Something")

    and then trap on c:

    If Not (c Is Nothing) Then....


    because the Range object allows the value of Nothing to be set, and for that matter is defined to be Nothing until it is set to something. A Double variable is set to 0 until something else is assigned to it.

    zaza
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    3

    Re: Differentiating between CDbl(0) and Nothing

    Okay well then what I'm trying to do is create a conditional that is aware of whether or not the value in my Double variable has been set to 0 or if it was the default 0. It doesn't sound as if this is possible with a Double variable.

  5. #5
    New Member
    Join Date
    Feb 2011
    Posts
    1

    Re: Differentiating between CDbl(0) and Nothing

    Just make your "Double" variable a "String" and then you can check to see if it is "Nothing" and when you need the "Double" value you can convert it or cast it... VB is usually very forgiving and will perform the cast implicitly for you.

    Dim myDbl as String

    If myDbl <> String.Empty Then....

    OR

    If myDbl IsNot Nothing Then....

    OR

    If myDbl <> Nothing Then....

    AND THEN

    myDbl = "12.99"

    Dim myBool as Boolean = SomeFunctionThatTakesDouble(CDbl(myDbl))
    ' again you could probably pass a String and VB will do the cast here
    ' but this is just a basic example.

    Shawn
    Last edited by si_the_geek; Feb 7th, 2011 at 06:19 PM. Reason: removed advertising link

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Differentiating between CDbl(0) and Nothing

    how are you assigning the value to the double?
    is it the content of a range (cell)?
    check for is nothing before assigning the numeric value to the double
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width