Results 1 to 4 of 4

Thread: correct value when using the minus sign

  1. #1
    wnarretto is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2012
    Posts
    4
    Reputation
    0

    correct value when using the minus sign

    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?

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,363
    Reputation
    1544
    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.

  3. #3
    Solitaire is offline VB.NET Forum Miyagee
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    New York
    Posts
    418
    Reputation
    162
    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

  4. #4
    wnarretto is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2012
    Posts
    4
    Reputation
    0
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking