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
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?
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
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.
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
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