correct value when using the minus sign

wnarretto

New member
Joined
Jun 7, 2012
Messages
4
Programming Experience
Beginner
hello .net world-
i am new to VB .Net. with that said... first project is to build a basic calculator. i have everything working the way it is supposed to work except for the minus button. when you say 1 - 2, your answer should be -1. for some reason i get 1 for my answer. the code is:
Private Sub cmdMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMinus.Click
Total1 = Total1 - Val(txtDisplay.Text)
txtDisplay.Clear()
End Sub

so my question is how to get the correct answer 1 - 2 = -1?
 
What is the original value of Total1? What is the value of txtDisplay.Text? What is the value of Val(txtDisplay.Text)? When an error occurs you don't just look at your code. You watch it in action and look at the inputs and outputs of each step to see whether they are what you expect them to be. As soon as you find a state that doesn;t match your expectations, you have found an issue.
 
I would add separate variables for each of the operators, and use TryParse instead of Val to convert the text into a number:

Dim op1, op2, total As Double
'op1 = first operator
Double.TryParse(txtDisplay.Text, op2)
total = op1 - op2
 
sorry for the late response-
thanks for the advice solitaire. i found the issue and it actually had to do with the equal button and not the minus button. :( thanks again for getting me to think. :)
 
Back
Top