|
-
Jun 11th, 2007, 11:34 AM
#1
Thread Starter
New Member
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
-
Jun 12th, 2007, 04:19 AM
#2
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?
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...
-
Jun 12th, 2007, 04:50 AM
#3
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
-
Jun 12th, 2007, 10:36 AM
#4
Thread Starter
New Member
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.
-
Feb 7th, 2011, 01:13 PM
#5
New Member
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
-
Feb 7th, 2011, 03:26 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|