我正在 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;
}
}
}
}
最佳答案
在您的代码中,您是 instantiating您的第二个类(class) 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; }
}
}
}
在上面的示例中,您可以看到我在 button1_Click
事件中实例化我的新类 public class Something
,然后使用 调用其中的属性>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; }
}
}
}
关于c# - Windows 窗体、方法和按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19191807/