2013-10-04 110 views
2

我正在測試Windows窗體上的銀行帳戶。易於開始的東西。在我的dep_Click方法中,我有一種方法,如果我點擊它,那麼aMtBox中的代碼就是我想要的。我想進一步擴展並嘗試使用某些屬性創建BankAccount類,等等。我使用我的方法來嘗試這樣做。我看到的所有東西看起來好像在控制檯應用程序中都可以,除非我不知道如何調用中的方法;也就是說,我可以使用我寫的代碼。窗體窗體,方法和按鈕

代碼是否完全錯誤,並且在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; 
     } 
    } 
} 
} 
+0

爲了讓我們需要知道你想要去的方向。請解釋你的最終目標是什麼。 –

+0

我看到你已經實例化了BankAccount,但它看起來並不像你在任何地方使用它?您需要像'a.IBa'或'a.Num1'這樣在'a'中調用您的屬性。 – Brian

+0

我做了一個編輯this.aMtBox.Text = a.withDrawl; –

回答

1

在代碼中,你是instantiating你的第二類,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; } 
    } 
    } 
} 
+0

感謝舉報,評論已被刪除,但下次請不要喂巨魔。只是默默地標記帖子。它**將**被刪除。 – Johan

+0

你明白了Johan。謝謝你的提示! – Brian

1
private void withdrawl_Click(object sender, EventArgs e) 
{ 
    this.aMtBox.Text = a.withDrawl().ToString(); 
} 

這將使它能夠通過編譯 - 它從回抽傳遞值300()方法到你的aMtBox文本框中。在depBox文本框中輸入一個值表示50,然後單擊dep按鈕將在aMtBox文本框中產生一個值350。另外,如果您單擊名爲withdrawl和dep的按鈕,則不會發生任何事情:嘗試在設計器中打開窗體並雙擊每個按鈕。您應該登陸withdrawl_Click,dep_Click事件處理程序。

編輯: 迴應回抽方法的返回類型的錯誤:

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