2

我正在 Windows 窗体上测试一个银行帐户。一些容易开始的事情。在我dep_Click的方法中,我有一种方法,如果我点击它,我会在aMtBox. 我想进一步扩展并尝试创建一个具有一些属性等的 BankAccount 类。我使用我的withdrawl_Click方法来尝试这样做。我看到的一切看起来在控制台应用程序中都可以,除了我不知道如何调用 ; 中的方法withdrawl_Click。也就是说,我是否能够使用我编写的代码。

代码是否完全错误并且 Windows 窗体应用程序中的某些内容有所不同?或者有没有我/没有理解的概念。请向我解释并清楚地说明这一点。

编辑:我更新了我的代码,但是它仍然给我一个关于withDrawl在底部附近的方法中需要返回类型的错误。仍然不确定。

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(iBa, num1);

    public Form1()
    {
        InitializeComponent();
        decimal iBa = 300.00m; // needs fixed to be more universal
        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);
        }
    }
    private void withdrawl_Click(object sender, EventArgs e)
    {

    }

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

        public decimal IBa
        {
            get
            {
                return iBa;
            }
        }
        public decimal Num1
        {
            get
            {
                return num1;
            }
        }
        public decimal Witht
        {
            get
            {
                return withT;
            }
        }

        public withDrawl(decimal n, decimal s )
        {
            iBa = n;
            num1 = s;
            withT = iBa - num1;
        }
    }
}
}
4

2 回答 2

1

在您的代码中,您正在使用以下行实例化您的第二个类BankAccount

BankAccount a = new BankAccount();

但是,你没有在任何地方使用它。根据您在原始帖子中所说的内容,要访问第二类中的属性,您可以这样称呼它们:

a.IBa;
a.Num1;
a.withDrawl;

下面是一个非常简单的例子:

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

      private void button1_Click(object sender, EventArgs e)
      {
         string aValue = String.Empty;
         Something a = new Something();
         a.SomeThingElse = aValue;
      }
   }

   public class Something
   {
      public string SomeThingElse
      {
         get { return SomeThingElse; }
         set { SomeThingElse = value; }
      }
   }
}

在上面的示例中,您可以看到我public class Somethingbutton1_Click事件中实例化了我的新类,然后通过 using 调用其中的属性a.SomethingElse = aValue,这是string我在初始类中定义的。在这种情况下,aValue=""因为它被初始化为String.Empty,即 '""'。

另外,附带说明一下,您可能希望将第二个类的抽象级别设置为public,因为如果不声明它public,它将默认为private(在这种情况下不会导致问题 - 这只是要记住的事情)。

我正在添加另一个示例 - 这个示例使用您提供的一些代码:

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 WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
     public Form1()
     {
         InitializeComponent();
     }

     private void dep_Click(object sender, EventArgs e)
     {
        try
        {
            decimal newBalance;
            BankAccount bankAccount = new BankAccount();  // Instantiate your class.
            bankAccount.DepositAmount = Convert.ToDecimal(depAmt.Text); 
            newBalance = (Convert.ToDecimal(currentBalance.Text)) + bankAccount.DepositAmount;
            currentBalance.Text = newBalance.ToString();
        }

        catch
        {
            MessageBox.Show("ERROR", "Oops, this isn't good!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

public class BankAccount
{
    decimal depositAmount;

    public decimal DepositAmount
    {
        get { return depositAmount; }
        set { depositAmount = value; }
    }
  }
}
于 2013-10-04T22:49:24.627 回答
1
private void withdrawl_Click(object sender, EventArgs e)
{
    this.aMtBox.Text = a.withDrawl().ToString();
}

这将使它编译 - 它将值 300 从 WithDrawl() 方法传递到您的 aMtBox 文本框。在 depBox 文本框中输入一个值,例如 50,然后单击 dep 按钮将在 aMtBox 文本框中产生一个值 350。

此外,如果您单击名为withdrawl 和dep 的按钮但没有任何反应:尝试在设计器中打开表单并双击每个按钮。您应该登陆withdrawl_Click、dep_Click 事件处理程序。

编辑:响应 Withdrawl 方法返回类型错误:

public decimal withDrawl(decimal n, decimal s )
{
    iBa = n;
    num1 = s;
    withT = iBa - num1;
    return withT;
}
于 2013-10-04T22:59:32.420 回答