|
-
May 12th, 2011, 12:54 PM
#1
Thread Starter
New Member
I need a runtime error when dividing by zero
Hey guys, I'm trying to make a program that uses Ohms circle to calculate varying data regarding electricity, I think that if you look at my code you'll get a good idea. The PROBLEM that I'm having is that I'm using On Error Resume Next so the program will automatically find the valid calculation to calculate the value for the variable, but what's happening is that when it divides by zero (the default value for variables with no value) it gets Infinity, which it has decided IS a value, but I don't want it to be, I need a runtime error so it will bypass that calculation and go on to the next one.
Code:
'Skip erroneous statements and continue on to next line of valid code
On Error Resume Next
'--------------------------
'VARIABLES
'--------------------------
'v = voltage
Dim v As Double = Double.Parse(txtVolt.Text)
'i = amperage
Dim i As Double = Double.Parse(txtAmp.Text)
'r = resistance
Dim r As Double = Double.Parse(txtOhm.Text)
'p = wattage
Dim p As Double = Double.Parse(txtWatt.Text)
'--------------------------
'CALCULATIONS
'--------------------------
'If nothing in amperage
If txtAmp.Text = "" Then
i = p / v
i = v / r
i = System.Math.Sqrt(p / r)
End If
'If nothing in voltage
If txtVolt.Text = "" Then
v = p / i
v = System.Math.Sqrt(p * r)
v = i * r
End If
'If nothing in ohms
If txtOhm.Text = "" Then
r = v / i
r = p / (i ^ 2)
r = (v ^ 2) / p
End If
'If nothing in watts
If txtWatt.Text = "" Then
p = v * i
p = (v ^ 2) / r
p = (i ^ 2) * r
End If
'--------------------------
'OUTPUT
'--------------------------
txtVolt.Text = v.ToString
txtAmp.Text = i.ToString
txtOhm.Text = r.ToString
txtWatt.Text = p.ToString
Big thanks to anyone who can help, I've been working on this for a few weeks with no good results.
-
May 12th, 2011, 01:33 PM
#2
Re: I need a runtime error when dividing by zero
Some ideas
Code:
'--------------------------
'VARIABLES
'--------------------------
'foo = intermediate placeholder
Dim foo As Double
'v = voltage
Dim v As Nullable(Of Double)
'i = amperage
Dim i As Nullable(Of Double)
'r = resistance
Dim r As Nullable(Of Double)
If Double.TryParse(txtVolt.Text, foo) Then
v = foo
End If
If Double.TryParse(txtAmp.Text, foo) Then
i = foo
End If
If Double.TryParse(txtOhm.Text, foo) Then
r = foo
End If
'i=v/r
If v.HasValue AndAlso r.HasValue Then
i = v / r
End If
'v=i*r
If i.HasValue AndAlso r.HasValue Then
v = i * r
End If
'r=v/i
If v.HasValue AndAlso i.HasValue Then
r = v / i
End If
-
May 12th, 2011, 01:38 PM
#3
Fanatic Member
Re: I need a runtime error when dividing by zero
I have no familiarity with using On Error Resume Next. What about just not using that. Just check for dividing by zero with whatever code you want and then skip to something else.
 Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.
"Persistence is the magic of success." Paramahansa Yogananda
-
May 12th, 2011, 02:49 PM
#4
Re: I need a runtime error when dividing by zero
You could wrap TryParse into a function which determines first if the string is a valid Integer or Double then determine if the value is above 0.
Example
Code:
Private Sub Button1_Click() Handles Button1.Click
Dim SomeValue As String = "0"
Dim MyInt As Integer = 0
If SomeValue.IsValidInteger(MyInt) Then
Console.WriteLine("valid int [{0}]", MyInt)
Else
Console.WriteLine("not valid int")
End If
SomeValue = "10"
If SomeValue.IsValidInteger(MyInt) Then
Console.WriteLine("valid int [{0}]", MyInt)
Else
Console.WriteLine("not valid int")
End If
SomeValue = ""
Dim MyDouble As Double = 0
If SomeValue.IsValidDouble(MyDouble) Then
Console.WriteLine("valid double [{0}]", MyDouble)
Else
Console.WriteLine("not valid double")
End If
SomeValue = "34.5"
If SomeValue.IsValidDouble(MyDouble) Then
Console.WriteLine("valid double [{0}]", MyDouble)
Else
Console.WriteLine("not valid double")
End If
End Sub
Method extensions go into a code module
Code:
''' <summary>
''' Checks to see if the string is a valid integer with a value above 0.
''' </summary>
''' <param name="sender"></param>
''' <param name="Value"></param>
''' <returns></returns>
''' <remarks></remarks>
<System.Diagnostics.DebuggerStepThrough()> _
<System.Runtime.CompilerServices.Extension()> _
Public Function IsValidInteger(ByVal sender As String, ByRef Value As Integer) As Boolean
Dim Result As Boolean = False
If Integer.TryParse(sender, Value) Then
If Value <> 0 Then
Result = True
End If
Else
Result = False
End If
Return Result
End Function
Code:
''' <summary>
''' Checks to see if the string is a valid double with a value above 0.
''' </summary>
''' <param name="sender"></param>
''' <param name="Value"></param>
''' <returns></returns>
''' <remarks></remarks>
<System.Diagnostics.DebuggerStepThrough()> _
<System.Runtime.CompilerServices.Extension()> _
Public Function IsValidDouble(ByVal sender As String, ByRef Value As Double) As Boolean
Dim Result As Boolean = False
If Double.TryParse(sender, Value) Then
If Value <> 0 Then
Result = True
End If
Else
Result = False
End If
Return Result
End Function
If you want to debug the extensions remove the following line
Code:
<System.Diagnostics.DebuggerStepThrough()>
-
May 13th, 2011, 12:38 PM
#5
Thread Starter
New Member
Re: I need a runtime error when dividing by zero
Thanks guys, I'll try those, I'm really trying to avoid using a bunch of if statements though, I'm trying to keep it relatively streamlined.
Entity: On Error Resume Next just tells the computer that if it hits an error, to just skip it and keep going, it's sort of the heart of my method right now (if it worked :P), what's supposed to happen is the program tries to perform calculation a, gets an error and doesn't assign a new value to the variable, performs calculation b, doesn't get an error and assigns that value to the variable, then performs calculation c, gets an error and doesn't assign a new value to the variable. That's why I need to get an error when I divide by zero.
Seriously though guys, thanks a ton for this.
Another idea that I had was to use some sort of a "check" statement, for example, if the Do While statement could stop within itself, I'd do this:
Code:
Do While v.HasValue = false
v = p / i
v = System.Math.Sqrt(p * r)
v = i * r
Loop
But the problem is that it doesn't check to see if v has value until the end of the loop, so it's essentially only performing v = i * r.
-
May 13th, 2011, 12:41 PM
#6
Junior Member
Re: I need a runtime error when dividing by zero
 Originally Posted by FireSpokes
But the problem is that it doesn't check to see if v has value until the end of the loop, so it's essentially only performing v = i * r.
Try using a pre-test loop. It will, obviously, check to see if 'v' has a value at the opening of the loop.
-
May 13th, 2011, 12:46 PM
#7
Thread Starter
New Member
Re: I need a runtime error when dividing by zero
Demon-Mask: I see you typing that next to me
(We're in class together)
I'll try that though
I figure that as long as I'm mentioning old ideas that I've had I'll throw this one out there:
Previously I had set the variables to only be declared if the text box they corresponded to had a value, or when a value was being assigned to that variable through calculations. This meant that in theory all variables either had a value from the text box, or didn't exist, so On Error Resume Next (I feel like I'm a little obsessed with using this statement...sorry) would skip over all of the bad calculations. The PROBLEM was that Vb.net was seeing that the declarations were within if statements, and On Error Resume Next wasn't running live, it ran prior to the click event, so any calculations using a variable that wasn't declared outside of an if statement were skipped entirely.
-
May 13th, 2011, 12:47 PM
#8
Junior Member
Re: I need a runtime error when dividing by zero
Why didn't we even consider a pre-test loop until now?
-
May 14th, 2011, 07:02 AM
#9
Re: I need a runtime error when dividing by zero
An update to my previous post that will get rid of divide by zero. Try-catch / on error should be avoided IF possible. Divide by zero can be avoided.
Code:
'--------------------------
'VARIABLES
'--------------------------
'foo = intermediate placeholder
Dim foo As Double
'v = voltage
Dim v As Nullable(Of Double)
'i = amperage
Dim i As Nullable(Of Double)
'r = resistance
Dim r As Nullable(Of Double)
If Double.TryParse(txtVolt.Text, foo) Then
v = foo
End If
If Double.TryParse(txtAmp.Text, foo) Then
i = foo
End If
If Double.TryParse(txtOhm.Text, foo) Then
r = foo
End If
'i=v/r
If v.HasValue AndAlso r.HasValue AndAlso r <> 0 Then
i = v / r
End If
'v=i*r
If i.HasValue AndAlso r.HasValue Then
v = i * r
End If
'r=v/i
If v.HasValue AndAlso i.HasValue AndAlso i <> 0 Then
r = v / i
End If
-
May 14th, 2011, 12:39 PM
#10
Re: I need a runtime error when dividing by zero
Fire
In the event that you haven't Resolved this yet, I
vote for EntityX's approach. Basically..
... before
Code:
'--------------------------
'CALCULATIONS
'--------------------------
'If nothing in amperage
If txtAmp.Text = "" Then
i = p / v
i = v / r
i = System.Math.Sqrt(p / r)
End If
... becomes
Code:
'--------------------------
'CALCULATIONS
'--------------------------
'If nothing in amperage
If txtAmp.Text = "" Then
If v = 0 or r = 0 Then
>> add code here to suit your needs
Else
i = p / v
i = v / r
i = System.Math.Sqrt(p / r)
End If
End If
Spoo
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
|