Hi, I'm having problems trying to figure out how to divide the value in a text box.

One textbox is named Purchase Price
I want to take that number and get it divided by 5. That answer will be the down payment.
I declared a const for downpayment for 5.
I did the ToDouble for the purchase price as well for downpayment. I'll put what I have down so far for the coding. I'm just having trouble finding my error.

Imports System.Convert
Public Class Form1
    Private Const DOWN_PAYMENT As Double = 5
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtPurchasePrice.Text = ""
        txtInterestRate.Text = ""
        txtDownPayment.Text = ""
    End Sub

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        Dim PurchasePrice As Double
        Dim InterestRate As Double
        Dim DownPayment As Double

        PurchasePrice = ToDouble(txtPurchasePrice.Text)

        InterestRate = ToDouble(txtInterestRate.Text) / 100

        DownPayment = ToDouble(txtDownPayment.Text)
        DownPayment = txtPurchasePrice.Text / 5
        DownPayment = System.Math.Abs(DownPayment)


    End Sub
End Class

If anyone could help me out please.

I thought adding

DownPayment = txtDownPayment.ToString("C")

Would help out to imported the value, but it gives me an error message of:

Value of type 'Char' cannnot be converted to 'Double'

I'm exactly sure what that means...

The following three statements get the values correctly

Dim PurchasePrice As Double = ToDouble(txtPurchasePrice.Text)
Dim InterestRate As Double = ToDouble(txtInterestRate.Text) / 100
Dim DownPayment As Double = ToDouble(txtDownPayment.Text)

However, if you are calculating the minimum down payment as a portion of the purchase price then there is no reason for the third line unless you intend to do something like

Dim PurchasePrice As Double = ToDouble(txtPurchasePrice.Text)
Dim InterestRate As Double = ToDouble(txtInterestRate.Text) / 100
Dim DownPayment As Double = ToDouble(txtDownPayment.Text)
Dim MinimumDown As Double = PurchasePrice / DOWN_PAYMENT

If DownPayment < MinimiumDown Then
    MsgBox("The minimum down payment is " & MinimumDown)
    etc.

And keep in mind that you should validate the text controls before converting to ensure that they are valid. That means

  • checking that the entered values are numeric
  • checking that the entered values are within a given range of allowable values

Thanks Jim, a comment to your last statement when you say the make sure the value are numeric is the statement.

"If IsNumeic(txtPurchasePrice.Text) Then"

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