该方法未被调用。我想也许有参数的东西?

时间:2013-10-05 01:13:07

标签: c#

WindowsFormsApplication2.Form1.BankAccount不包含withBox的定义,并且没有可以找到接受类型withBox的第一个参数的扩展方法WindowsFormsApplication2.Form1.BankAccount(您是否缺少using指令或汇编参考?)

这是num1 = Convert.ToDecimal(this.withBox.Text);代码行之一。

早些时候我问过这个问题,并对我的问题进行了更多的研究。我相信这次我做的一切都是正确的。但仍然有错误。我不确定我现在做错了什么。

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;
        }
    }
}

}

1 个答案:

答案 0 :(得分:2)

您在未定义此属性的this.WithBox.Text内使用BankAccount。我想这是Form1中定义的。

您应该将BankAccount构造函数更改为此类

public BankAccount(decimal number)
{
    iBa = 300.00m;
    num1 = number;
    balance = iBa - num1;
}

然后创建BankAccount以这种方式传递表单中的值:

BankAccount a = null;

public Form1()
{
    InitializeComponent();
    decimal iBa = 300.00m;
    this.aMtBox.Text = iBa.ToString();
    a = new BankAccount(Convert.ToDecimal(this.withBox.Text));
}

也许在表单初始化时从withBox获取值没有多大意义,没有应用程序的上下文,但是你会明白这一点。另一种方法是在必要时将Num1的属性设置为BankAccount