operator *不能应用于操作数C#

时间:2017-11-05 19:05:37

标签: c#

尝试制作贷款申请,我将此作为错误

  

operator *不能应用于textbox和int类型的操作数   operator *不能应用于textbox类型和double

的操作数

我的代码看起来像这样

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 MyApp3
{
    public partial class LoanApplication : Form
    {
        double InterestRate, monthlyInterestRate, loanAmount, monthlyPayment, TotalPayment;
        int NoOfyears;

        private void button1_Click(object sender, EventArgs e)
        {
            InterestRate = Convert.ToDouble(rate.Text);
            monthlyInterestRate = InterestRate / 1200;
            NoOfyears = Convert.ToInt32(noOfYrs.Text);
            loanAmount = Convert.ToDouble(txtLoanAmt.Text);

            monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.Pow(1 + monthlyInterestRate,noOfYrs * 12)); // This Line

            iMonthlyPayment = Convert.ToString(monthlyPayment);
            iMonthlyPayment = String.Format("{0:C}", monthlyPayment);
            monthly_payment.Text = (iMonthlyPayment);

            TotalPayment = monthlyPayment * noOfYrs * 12; // This Line
            iTotalPayment = String.Format("{0:C}", TotalPayment);
            total_payment.Text = (iTotalPayment);

            txtLoanAmt.Text = String.Format("{0:C}", txtLoanAmt.Text);
        }

        string iMonthlyPayment, iTotalPayment;
        public LoanApplication()
        {
            InitializeComponent();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void LoanApplication_Load(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
        }
    }
}

我到底错过了什么?

2 个答案:

答案 0 :(得分:0)

您正在尝试使用textbox * int。文本框是对象,你不能将它乘以它的双倍。您需要首先从文本框获取值并将其转换为int int或Double。例如。 int.Parse(myTextbox.Text)* myInt

答案 1 :(得分:0)

在有问题的行中,您使用的是变量noOfYrs,它是一个TextBlock,而不是NoOfyears,它是一个整数。