0

I keep getting an error message stating that I cannot convert a string to a double in reference to line 39. Can someone review the code and let me know where I'm off? I feel that maybe line 25 should list double for the variable "R."

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Present_Value
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //Declare global variables. 
       /* double F = int.Parse(futureTextBox.Text);
        double R = int.Parse(interestTextBox.Text);
        double N = int.Parse(sitTextBox.Text);*/

        private double CalculateData(int F, int R, int N)
        {
            double P = F/(1 + R)*N;
            return P ;

        }



        private void button1_Click(object sender, EventArgs e)
        {

            int n = int.Parse(sitTextBox.Text);
            int f = int.Parse(futureTextBox.Text);
            int r = double.Parse(intTextBox.Text);

            presentValuelabel.Text = CalculateData(f,r,n).ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            //Close this form.
                this.Close();
        }
    }
}
3
  • You're declaring int variable but trying to assign double with double.Parse() method. Try to change double.Parse() to int.Parse(). Commented Jun 18, 2017 at 23:19
  • Which is line 39 and line 25? You should post a minimal reproducible example and make it easy for us to answer. Commented Jun 19, 2017 at 0:05
  • Do keep in mind that the calculation F/(1 + R)*N (as it stands in your question) is purely being calculated with integer math. So, for example, the result of (1 / (1 + 2) * 3) is 0 using integer math. But if I change to doubles then (1.0 / (1.0 + 2.0) * 3.0) gives 1.0. Commented Jun 19, 2017 at 0:09

2 Answers 2

1

There are two way to edit.

Method 1) You should edit CalculateData function. int R => double R

private double CalculateData(int F, double R, int N)
{
    double P = F / (1 + R) * N;
    return P;

}
private void button1_Click(object sender, EventArgs e)
    {

        int n = int.Parse(sitTextBox.Text);
        int f = int.Parse(futureTextBox.Text);
        double r = double.Parse(intTextBox.Text);

        presentValuelabel.Text = CalculateData(f, r, n).ToString();
    }

Method 2) You should edit button1_Click function. double.Parse(intTextBox.Text) => int.Parse(intTextBox.Text)

private void button1_Click(object sender, EventArgs e)
{

    int n = int.Parse(sitTextBox.Text);
    int f = int.Parse(futureTextBox.Text);
    int r = int.Parse(intTextBox.Text);

    presentValuelabel.Text = CalculateData(f, r, n).ToString();
}

I hope it will help you.

Sign up to request clarification or add additional context in comments.

Comments

0

When dealing with user input (from the textboxes) you should use the .TryParse() methods instead of .Parse(). We cannot trust the users...

Also, if your input might be a real number then culture is an isse, e.g. 3.4 or 3,4.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.