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

Dani AI

Generated

The core issue is evaluating a text expression (like "A+B") and turning it into a numeric result. provided the inputs; and correctly pointed toward string parsing and index pitfalls; recommended isolating parsing/evaluation into a separate routine. Two practical, safer approaches follow.

A compact evaluator that handles either literal numbers or placeholder letters (A/B) is to replace placeholders with the numeric values and let the data-expression engine compute the result. Validation is important: reject unexpected characters before evaluating.

Imports System.Data
Imports System.Globalization
Imports System.Text.RegularExpressions

Private Function ComputeResult(expr As String, aVal As Double, bVal As Double) As Double
    expr = expr.Replace("A", aVal.ToString(CultureInfo.InvariantCulture)).
                Replace("a", aVal.ToString(CultureInfo.InvariantCulture)).
                Replace("B", bVal.ToString(CultureInfo.InvariantCulture)).
                Replace("b", bVal.ToString(CultureInfo.InvariantCulture))

    If Not Regex.IsMatch(expr, "^[0-9\.\+\-\*\/\(\)\s]+$") Then
        Throw New ArgumentException("Invalid characters in expression.")
    End If

    Dim dt As New DataTable()
    Dim raw = dt.Compute(expr, Nothing)
    Return Convert.ToDouble(raw, CultureInfo.InvariantCulture)
End Function

Use Double.TryParse to convert TextBox inputs into numeric values before calling the function (avoids CDbl exceptions). DataTable.Compute is suitable for basic arithmetic; details are in the System.Data.DataTable.Compute docs. Prefer TryParse over direct conversion to handle bad input safely (Double.TryParse).

Troubleshooting notes: trim whitespace, normalize decimal separators with InvariantCulture, guard against division-by-zero, and remember the index-base mismatch when using low-level string functions (as pointed out). Splitting/parsing by operator or using a Regex-based parser gives finer control if expression validation or extra operators are needed.

Recommended Answers

All 6 Replies

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

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.