Asked by Sheng Wei

Why my vb calculation appears infinity?
The calcluator is set to calculate current, resistance and power.
the followings are the code for the calculator:
Public Class Form3
Dim Black As Integer = 0
Dim Brown As Integer = 1
Dim Red As Integer = 2
Dim Orange As Integer = 3
Dim Yellow As Integer = 4
Dim Green As Integer = 5
Dim Blue As Integer = 6
Dim Violet As Integer = 7
Dim Gray As Integer = 8
Dim White As Integer = 9
Dim Gold As Integer = 5
Dim Silver As Integer = 10
Dim None As Integer = 20
----------------------------------------
Private Sub dud1_SelectedItemChanged(sender As Object, e As EventArgs) Handles dud1.SelectedItemChanged
If dud1.SelectedItem = "Black" Then
txt7.BackColor = Color.Black
txt7.Text = Black
ElseIf dud1.SelectedItem = "Brown" Then
txt7.BackColor = Color.Brown
txt7.Text = Brown
ElseIf dud1.SelectedItem = "Red" Then
txt7.BackColor = Color.Red
txt7.Text = Red
ElseIf dud1.SelectedItem = "Orange" Then
txt7.BackColor = Color.Orange
txt7.Text = Orange
ElseIf dud1.SelectedItem = "Yellow" Then
txt7.BackColor = Color.Yellow
txt7.Text = Yellow
ElseIf dud1.SelectedItem = "Green" Then
txt7.BackColor = Color.Green
txt7.Text = Green
ElseIf dud1.SelectedItem = "Blue" Then
txt7.BackColor = Color.Blue
txt7.Text = Blue
ElseIf dud1.SelectedItem = "Violet" Then
txt7.BackColor = Color.Violet
txt7.Text = Violet
ElseIf dud1.SelectedItem = "Gray" Then
txt7.BackColor = Color.Gray
txt7.Text = Gray
ElseIf dud1.SelectedItem = "White" Then
txt7.BackColor = Color.White
txt7.Text = White
End If
End Sub
----------------------------------------
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
txt1.Text = Val(txt7.Text + txt8.Text) * 10 ^ (Val(txt9.Text))
txt2.Text = (Val(txt7.Text + txt8.Text) * 10 ^ (Val(txt9.Text))) * Val(Val(txt10.Text) / 100)
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
txt6.Text = Val(txt5.Text) / Val(txt12.Text)
txt11.Text = Val(txt5.Text) * Val(txt6.Text)
txt12.Text = Val(1 / (Val(1 / (txt3.Text)) + Val(1 / txt4.Text)))
End Sub
----------------------------------------
The calculation appears "infinity" for current when press "calculate" for the first time. After pressing it for second time then the actual no. appear.
If I change the no. of voltage then press calculate, it will show the previous no. The actual no. appears only when the "calculate" button pressed twice.
What's the problem?

Answers

Answered by Steve
Looks like you have some unintialized variables. Dividing by txt12 before giving it a value will use its default value of zero.
Answered by Sheng Wei
Bug solved. Thank you. ^^
There are no AI answers yet. The ability to request AI answers is coming soon!

Related Questions