Author Topic: anyone here know VB? weird bug  (Read 3048 times)

0 Members and 1 Guest are viewing this topic.

Offline Topgun

  • 210
anyone here know VB? weird bug
this crashes at runtime:

Code: [Select]
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)
« Last Edit: February 01, 2010, 09:26:52 am by Topgun »

 

Offline Sushi

  • Art Critic
  • 211
Re: anyone here know VB? weird bug
What does it crash with?

 

Offline Topgun

  • 210
Re: anyone here know VB? weird bug
Quote
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.

 

Offline The E

  • He's Ebeneezer Goode
  • 213
  • Nothing personal, just tech support.
    • Steam
    • Twitter
Re: anyone here know VB? weird bug
Well, you should make sure that you never divide through zero. That should be obvious.
If I'm just aching this can't go on
I came from chasing dreams to feel alone
There must be changes, miss to feel strong
I really need lifе to touch me
--Evergrey, Where August Mourns

 

Offline Sushi

  • Art Critic
  • 211
Re: anyone here know VB? weird bug
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.

 

Offline Topgun

  • 210
Re: anyone here know VB? weird bug
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?

 

Offline Sushi

  • Art Critic
  • 211
Re: anyone here know VB? weird bug
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.

 
Re: anyone here know VB? weird bug
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.

 

Offline The E

  • He's Ebeneezer Goode
  • 213
  • Nothing personal, just tech support.
    • Steam
    • Twitter
Re: anyone here know VB? weird bug
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.
If I'm just aching this can't go on
I came from chasing dreams to feel alone
There must be changes, miss to feel strong
I really need lifе to touch me
--Evergrey, Where August Mourns