问题在于
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)
{
}
}
}
答案 0 :(得分: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);
答案 1 :(得分: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;
}