
October 9th, 2012, 02:07 PM
|
 |
Type Cast Exception
|
|
Join Date: Apr 2004
Location: OAKLAND CA | Adam's Point (Fairyland)
|
|
Quote: | Originally Posted by couttsj You cannot divide by zero; it will always produce an error.
J.A. Coutts |
That is true, however, the IIf should use the first, not the second condition.
However, this is not the case. In a beautiful example of flummoxing engineering VB actually evaluates both even though it's completely unnecessary to do so!
Quote: | Originally Posted by VBA HELP
IIf always evaluates both truepart and falsepart, even though it returns only one of them. Because of this, you should watch for undesirable side effects. For example, if evaluating falsepart results in a division by zero error, an error occurs even if expr is True.
|
Replace the IIf with a traditional If Then Else block instead.
Code:
If (s3breakdown * s4breakdown = 0) Then
s5breakdown = 0
Else
s5breakdown = s3breakdown - s4breakdown / s4breakdown
End If
Be sure to pay attention to operator precedence and also understand that this is the same as
s5breakdown = s3breakdown - (s4breakdown / s4breakdown)
and not
s5breakdown = (s3breakdown - s4breakdown) / s4breakdown
THIS IS IMPORTANT: as you have written it, this is also the same as:
s5breakdown = s3breakdown - 1
because n / n always equals 1 for all times when n is not 0, and if n is 0 it's mathematically impossible
__________________
medialint.com
“Today you are You, that is truer than true. There is no one alive who is Youer than You.” - Dr. Seuss
Last edited by medialint : October 9th, 2012 at 02:15 PM.
|