## ceteris

im new in vb and i just create a simple calculation

if i click the button for compute it will display the error

``````Dim price As Decimal = txtprice.Text
Dim quantity As Integer = txtquantity.Text
Dim totalprice As Decimal = txttotal.Text
totalprice = quantity * price``````

working code below

``txttotal.Text = Val(txtprice.Text) * Val(txtquantity.Text)``

## makdu

``````dim totprice as decimal
totprice= Val(txtprice.Text) * Val(txtquantity.Text)
txttotal.Text=totprice``````

## Reverend Jim 2,484

You should be using a try/catch block like this

``````Dim totprice As Decimal

Try
totprice = CDec(txtPrice.Text)
MsgBox("value is " & totprice.ToString)
Catch ex As System.InvalidCastException
MsgBox("not a number")
End Try``````

If the conversion is not successful the "Catch" portion will be executed. Another way to test without Try/Catch is

``````If IsNumeric(txtPrice.Text) Then
totprice = CDec(txtPrice.Text)
Else
MsgBox("Not a number")
End If``````

But Try/Catch is probably better.

commented: Good solution... +5

## codeorder 197

I would try to skip the entire Try/Catch on this issue and check if a numeric value.

``````If IsNumeric(txtPrice.Text) Then
'// do stuff here
Else
MsgBox("Not a number")
End If``````

Just my 2.cents worth.

## M.Waqas Aslam 67

hello !
try this

``````Dim price As Decimal = val(txtprice.Text)
Dim quantity As Integer = val(txtquantity.Text)
Dim totalprice As Decimal = val(txttotal.Text)
totalprice = val(val(quantity) * val(price))``````

this will work fine ,

Regards

## codeorder 197

Not to start anything here, though I do believe that "val" is vb6- related and could possibly cause issues in future .Net Frameworks.

## M.Waqas Aslam 67

no , val does not cause any issue in vb.net as i am continuously using it in about 2.5 years , :)

Regards