2

问题出在

num1 = Convert.ToDecimal(this.withBox.Text);

bankAccount()

我正在尝试将其设置为您可以在 withBox 文本框中输入任何小数点的位置,当您单击按钮时,它将为您提供aMtBox. 我不确定我做错了什么。

是给我这个错误,但我不知道为什么?

我想要的是让 num1 等于我在 withBox 中输入的任何内容。那是我的最终目标。

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

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
    BankAccount a = new BankAccount();

    public Form1()
    {
        InitializeComponent();
        decimal iBa = 300.00m;
        this.aMtBox.Text = iBa.ToString();
    }
    private void dep_Click(object sender, EventArgs e)
    {
        try
        {
            decimal num1 = 0.00m;
            decimal iBa = 300.00m;
            num1 = Convert.ToDecimal(this.depBox.Text);
            decimal total = num1 + iBa;
            this.aMtBox.Text = total.ToString();
        }
        catch
        {
            MessageBox.Show("ERROR", "Oops, this isn't good!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    public void withdrawl_Click(object sender, EventArgs e)
    {
        this.aMtBox.Text = a.Balance.ToString();
    }

    public class BankAccount
    {
        decimal balance;
        decimal iBa;
        decimal num1;

        public decimal Balance
        {
            get { return balance; }
        }
        public decimal IBa
        {
            get { return iBa; }
        }
        public decimal Num1
        {
            get { return num1; }
        }

        public BankAccount()
        {
            iBa = 300.00m;
            num1 = Convert.ToDecimal(this.withBox.Text);
            balance = iBa - num1;
        }
    }

    private void withBox_TextChanged(object sender, EventArgs e)
    {

    }
}

}

4

2 回答 2

2

this指类的当前实例。this在您的BankAccount构造函数中指的是BankAccount.. 而不是Form. 因此,您无法withBox从内部访问BankAccount

你需要做的..是将文本框实例传入。有点像这样:

public class BankAccount {
    public BankAccount(TextBox withBox) { // Pass it in
        iBa = 300.00m;
        num1 = Convert.ToDecimal(withBox.Text);
        balance = iBa - num1;
    }
    // ...the rest of the class
}

然后,在你的Form.. 中创建你的BankAccount这样的:

BankAccount a = new BankAccount(withBox);
于 2013-10-05T04:56:48.283 回答
0

withBox不是该类的成员,BankAccount但您正试图访问它。我假设withBox是班级成员Form1

    public BankAccount()
    {
        iBa = 300.00m;
        // this.withBox is no good, no such property/member variable exists
        num1 = Convert.ToDecimal(this.withBox.Text);
        balance = iBa - num1;
    }
于 2013-10-05T05:04:10.587 回答