I am very new in VB.Net. I can read the integer with no problem at all but I just cannot extract the equation I enter. I enter A+B, I can read it no problem, but how do I extract the equation information to make the two number to add together to show in answer. I try parsing but it is not helping. I got no idea how to do this, need help.

Below is my code:

Public Class Form1

    Dim Number1 As Double
    Dim Number2 As Double
    Dim A As Double
    Dim B As Double
    Dim Equation As String
    Dim Answer As Double

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Number1 = TextBox1.Text
        Number2 = TextBox2.Text
        Equation = TextBox3.Text
        A = Number1
        B = Number2
        MsgBox(Equation)   *can read equation enter 
        Answer = Equation     *can't use the equation
        MsgBox(Answer)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
    End Sub

    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
    End Sub

End Class

Edited 6 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

You're going to have to do a check for each symbol and I'm sure you could probably come up with a more dynamic way of doing this to incorporate multiple operations.

But here's one of many options that can be done in order to do addition.

Dim sTemp As String = TextBox3.Text
            Dim Answer As Double = 0
            Dim Number1 As Double = 0
            Dim Number2 As Double = 0
            If sTemp.IndexOf("+") > 0 Then
                Number1 = (CDbl(Mid(sTemp, sTemp.IndexOf("+") + 1, sTemp.Length)))
                Number2 = (CDbl(Mid(sTemp, 1, sTemp.IndexOf("+"))))
            End If

            Dim Equation As String = sTemp

            MsgBox(Equation) 'can read equation enter
            Answer = Number1 + Number2 ' can 't use the equation
            MsgBox(Answer)

As a side note, please use the code tags when entering code into the message board. Makes it much easier to read, thanks!

HTH

Edited 6 Years Ago by Stevoni: n/a

I can't debug the program. Each time I test the program it shows that

Number1 = (CDbl(Mid(sTemp, sTemp.IndexOf("+"), sTemp.Length)))

It says "Conversion from string "A+B" to type 'Double' is not valid."

Since I am new in VB programming, I not very understand the coding.

Sorry, I typed wrong, it should be "-1" not "+1."

I'm currently reinstalling VS, so I'm going with my previous knowledge on this one. Sorry again.

Edited 6 Years Ago by Stevoni: n/a

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Number1 = TextBox1.Text
        Number2 = TextBox2.Text
        Equation = TextBox3.Text
        A = Number1
        B = Number2
        MsgBox(Equation)   *can read equation enter 
        Answer = Equation     *can't use the equation
        MsgBox(Answer)

    End Sub

Parse the Equation for Number Operator Number, then Evaluate the result. Do it in separate function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       '....
        MsgBox(Equation)   'can read equation enter 
        Answer = Evaluate (Equation)     'Evaluate the equation
        MsgBox(Answer)
    End Sub

    Private Function Evaluate ( sEquation As String ) As Double 
          '....
          'Parse the string and get the numbers and operator
          'Evaluate the result
          'return the result
    End Function

Stevoni has shown to parse the string, but u try for all the case, because user can enter A+B, A-B, A/B ...

I already change to -1 but it still have problem.

Number1 = (CDbl(Mid(sTemp, sTemp.IndexOf("+") - 1, sTemp.Length)))

It says that "Argument "Start" must be greater than zero"

Actually, if you are going to do it that way, instead of "-1" it should be "+2" to get the number after the "+" sign. The "start" argument of the Mid function is 1-based while the indexof property of a string is 0-based.

stemp.Substring(stemp.IndexOf("+") + 1, stemp.Length - stemp.IndexOf("+") - 1)

will achieve the same result

This article has been dead for over six months. Start a new discussion instead.