Hard Light Productions Forums
Off-Topic Discussion => Programming => Topic started by: Topgun on February 01, 2010, 09:19:37 am
-
this crashes at runtime:
Option Strict On
Public Class Form1
Private Sub valPoint1X_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles valPoint1X.ValueChanged
txtSlope.Text = Convert.ToString((valPoint2Y.Value - valPoint1Y.Value) / (valPoint2X.Value - valPoint1X.Value))
txtYintercept.Text = Convert.ToString(valPoint1Y.Value - (((valPoint2Y.Value - valPoint1Y.Value) / (valPoint2X.Value - valPoint1X.Value)) * valPoint1X.Value))
End Sub
Private Sub valPoint1Y_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles valPoint1Y.ValueChanged
txtSlope.Text = Convert.ToString((valPoint2Y.Value - valPoint1Y.Value) / (valPoint2X.Value - valPoint1X.Value))
txtYintercept.Text = Convert.ToString(valPoint1Y.Value - (((valPoint2Y.Value - valPoint1Y.Value) / (valPoint2X.Value - valPoint1X.Value)) * valPoint1X.Value))
End Sub
Private Sub valPoint2X_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles valPoint2X.ValueChanged
txtSlope.Text = Convert.ToString((valPoint2Y.Value - valPoint1Y.Value) / (valPoint2X.Value - valPoint1X.Value))
txtYintercept.Text = Convert.ToString(valPoint1Y.Value - (((valPoint2Y.Value - valPoint1Y.Value) / (valPoint2X.Value - valPoint1X.Value)) * valPoint1X.Value))
End Sub
Private Sub valPoint2Y_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles valPoint2Y.ValueChanged
txtSlope.Text = Convert.ToString((valPoint2Y.Value - valPoint1Y.Value) / (valPoint2X.Value - valPoint1X.Value))
txtYintercept.Text = Convert.ToString(valPoint1Y.Value - (((valPoint2Y.Value - valPoint1Y.Value) / (valPoint2X.Value - valPoint1X.Value)) * valPoint1X.Value))
End Sub
End Class
interestingly, if I take out the last two methods, it works fine, just doesn't update when I change the value on the respective controls (obviously)
-
What does it crash with?
-
An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.VisualBasic.dll
Additional information: An error occurred creating the form. See Exception.InnerException for details. The error is: Attempted to divide by zero.
-
Well, you should make sure that you never divide through zero. That should be obvious.
-
Well, you should make sure that you never divide through zero. That should be obvious.
Yep, all of your methods should be handling that case (including the first two) and aren't. Anytime your two points have the same X value, it's going to choke when trying to calculate the slope.
-
its probably that, I am used to retuning void or false when dividing by zero, but this vb, and I don't know how to do that yet.
i do have to ask though, why doesn't it complain in the first two methods?
-
its probably that, I am used to retuning void or false when dividing by zero, but this vb, and I don't know how to do that yet.
What you get if you don't preemptively handle the div-by-0 case is an exception. If you don't handle the exception with a try-catch, it crashes your program.
i do have to ask though, why doesn't it complain in the first two methods?
I wonder the same thing. :) Run-time errors can be funny like that. Most likely, you just got lucky and never hit the right run-time scenario to trigger them.
-
My guess as to why it doesn't crash in the first two cases is a matter of timing.
IIRC, VB.net can actually return 'Not a number'. I suspect when it initializes, the text boxes start out blank, and every time they get changed during initialization, the changed methods get called.
When it calls the first two, point2x isn't initialized. It returns not a number, so (valPoint2X.Value - valPoint1X.Value)) * valPoint1X.Value)) is also not a number. Once point2x get initialized though, all the values in the divisor are numbers, and they add up to zero. Dividing by not a number just gets you not a number. Dividing by zero....is bad.
-
In any case, the lesson is to check your numbers for sanity before doing math with them. Even not-a-number results should be filtered out and dealt with.