Variables are concatenated instead of adding

2

I am making a solution in Visual Studio 2017 that emulates a simple type of invoice, the user enters the values through a textbox, gives the "Add" button and the data is entered in a Data Grid View, then by pressing the "Calculate" button, the total amount is calculated, the VAT of that amount and then the total amount of the amount and VAT. The problem is that the calculation of the total (amount + VAT) is not added, only both concatenated values are printed. Why could this be happening?

This is my code:

    Public Class Form1
Dim importePR As Double

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

    importePR = txtcantidad.Text * txtprecio.Text
    Me.DGV1.Rows.Add(txtdescrip.Text, txtcantidad.Text, txtprecio.Text, importePR)
    txtdescrip.Clear()
    txtcantidad.Clear()
    txtprecio.Clear()



End Sub


Private Sub btnBorrar_Click(sender As Object, e As EventArgs) Handles btnBorrar.Click

    DGV1.Rows.Clear()


    txtdescrip.Clear()
    txtcantidad.Clear()
    txtprecio.Clear()
    txtimporte.Clear()
    txtIVA.Clear()
    txttotal.Clear()

    txtdescrip.Text = " "
    txtcantidad.Text = " "
    txtprecio.Text = " "




End Sub

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGV1.CellContentClick




End Sub

Private Sub btnCalcular_Click(sender As Object, e As EventArgs) Handles btnCalcular.Click
    Dim importe As Double
    Dim ImpTotal As Double
    Dim fila As DataGridViewRow = New DataGridViewRow()

    For Each fila In DGV1.Rows
        importe += Convert.ToDouble(fila.Cells("Importe").Value)

    Next

    txtimporte.Text = Convert.ToString(importe)

    txtIVA.Text = txtimporte.Text * 0.16

    ImpTotal = txtimporte.Text + txtIVA.Text
    txttotal.Text = ImpTotal

End Sub

End Class

    
asked by Erick Finn 19.10.2018 в 21:33
source

3 answers

0

This happens simply because the operator + is overloaded in VB.

Therefore, it works differently depending on the type of objects you have to "add"

In your case, you are adding two strings , therefore, the operator + does not add its content, but it concatenates it, causing the chains to "add up" side by side.

To obtain the sum of two numbers, but that are contained in variables of type string (and the property text of a textbox , it is just a string), what you have to do is convert it to whole .

For that there are several methods, but assuming that your textbox is correct, you can use Int32.Parse() .

Remember to check that the values are correct, otherwise Parse will throw an exception. To control, you can use Int32.TryParse() .

    
answered by 19.10.2018 в 21:40
0

You are adding variables of type text, such as the content of a textbox. You have to before adding com +, convert them to numbers, such as double, inter, etc, with the functions that the VB has for this, such as CINT () etc.

From there you can add calmly.

    
answered by 05.11.2018 в 18:13
0

Activate the Error Control and do not Analyze it, for the Problem of the Sum, you have to do the Sum with the Strings Converted to Integers ...

Try this rectified code ...

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

    importePR = txtcantidad.Text * txtprecio.Text
    Me.DGV1.Rows.Add(txtdescrip.Text, txtcantidad.Text, txtprecio.Text, importePR)
    txtdescrip.Clear()
    txtcantidad.Clear()
    txtprecio.Clear()



End Sub


Private Sub btnBorrar_Click(sender As Object, e As EventArgs) Handles btnBorrar.Click

    DGV1.Rows.Clear()


    txtdescrip.Clear()
    txtcantidad.Clear()
    txtprecio.Clear()
    txtimporte.Clear()
    txtIVA.Clear()
    txttotal.Clear()

    txtdescrip.Text = " "
    txtcantidad.Text = "1"
    txtprecio.Text = "0.00"




End Sub

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGV1.CellContentClick




End Sub

Private Sub btnCalcular_Click(sender As Object, e As EventArgs) Handles btnCalcular.Click
On Error Resume Next
    Dim importe As Double
    Dim ImpTotal As Double
    Dim fila As DataGridViewRow = New DataGridViewRow()

    For Each fila In DGV1.Rows
        importe += Convert.ToDouble(fila.Cells("Importe").Value)

    Next

    txtimporte.Text = Convert.ToString(importe)

    txtIVA.Text = CDbl(txtimporte.Text) * 0.16

    ImpTotal = CDbl(txtimporte.Text) + CDbl(txtIVA.Text)
    txttotal.Text = ImpTotal

End Sub
    
answered by 05.11.2018 в 17:37