Jared Venson
Jared Venson

Reputation: 57

Why won't my calculator compute division or multiplication?

I am creating a calculator in a VB.NET windows application. All of the operators (addition and subtraction) are working fine, however, when I try to compute using division or multiplication, line 39 throws a System.Data.SyntaxErrorException. This is the line that throws the exception:

Dim result = New DataTable().Compute(equation, Nothing) 

Does any one have a clue regarding why this occurs? Here is the rest of the code:

Public Class Form1
    Private Sub ButtonClickMethod(sender As Object, e As EventArgs)           Handles num0.Click, num1.Click, num2.Click, num3.Click, num4.Click, num5.Click, num6.Click, num7.Click, num8.Click, num9.Click, opdivision.Click, opmultiply.Click, opdecimal.Click, opclear.Click, opminus.Click, opadd.Click, opequal.Click
        Dim button As Button = CType(sender, Button)
        If button.Name = "num1" Then
            TextBox1.Text = TextBox1.Text + "1"
        End If
        If button.Name = "num2" Then
            TextBox1.Text = TextBox1.Text + "2"
        End If
        If button.Name = "num3" Then
            TextBox1.Text = TextBox1.Text + "3"
        End If
        If button.Name = "num4" Then
            TextBox1.Text = TextBox1.Text + "4"
        End If
        If button.Name = "num5" Then
            TextBox1.Text = TextBox1.Text + "5"
        End If
        If button.Name = "num6" Then
            TextBox1.Text = TextBox1.Text + "6"
        End If
        If button.Name = "num7" Then
            TextBox1.Text = TextBox1.Text + "7"
        End If
        If button.Name = "num8" Then
            TextBox1.Text = TextBox1.Text + "8"
        End If
        If button.Name = "num9" Then
            TextBox1.Text = TextBox1.Text + "9"
        End If
        If button.Name = "num0" Then
            TextBox1.Text = TextBox1.Text + "0"
        End If
        If button.Name = "opdecimal" Then
            TextBox1.Text = TextBox1.Text + "."
        End If
        If button.Name = "opequal" Then
            Dim equation As String = TextBox1.Text
            Dim result = New DataTable().Compute(equation, Nothing)
            boxresult.Text = result
        End If
        If button.Name = "opminus" Then
            TextBox1.Text = TextBox1.Text + "-"
            boxoperator.Text = boxoperator.Text + "-"
        End If
        If button.Name = "opmultiply" Then
            TextBox1.Text = TextBox1.Text + "x"
            boxoperator.Text = boxoperator.Text + "x"
        End If
        If button.Name = "opdivision" Then
            TextBox1.Text = TextBox1.Text + "÷"
            boxoperator.Text = boxoperator.Text + "÷"
        End If
        If button.Name = "opadd" Then
            TextBox1.Text = TextBox1.Text + "+"
            boxoperator.Text = boxoperator.Text + "+"
        End If
        If button.Name = "opclear" Then
            TextBox1.Clear()
        End If
    End Sub

    Private Sub opbackspace_Click(sender As Object, e As EventArgs) Handles opbackspace.Click
        TextBox1.Text = TextBox1.Text.Remove(TextBox1.Text.Count - 1)
    End Sub
End Class

Upvotes: 1

Views: 115

Answers (1)

Cepheus
Cepheus

Reputation: 589

You have to use different characters for multiplication and division:

If button.Name = "opmultiply" Then
    TextBox1.Text = TextBox1.Text + "*"
    boxoperator.Text = boxoperator.Text + "x"
End If
If button.Name = "opdivision" Then
    TextBox1.Text = TextBox1.Text + "/"
    boxoperator.Text = boxoperator.Text + "÷"
End If

Upvotes: 1

Related Questions