2016-12-01 9 views
0

を使用した後にクラスのプロパティにアクセスする私はC#の初心者です。クラスの使用に基づいて継承と多相に関連する質問があります。私が働いている課題は銀行口座で、私は3つのクラスを使用する必要があります.1つは基本クラスであり、他の2つはクラスを派生したクラスです。

基本クラスは "BankAccount"と呼ばれ、2つの派生クラスは "CheckingAccount"と "SavingsAccount"です。さらに、私は "CheckingAccount"と "SavingAccount"のためのオブジェクトを宣言し、それらはListクラスに格納されています。その後、コンボボックスの "SelectedIndexChange"イベントを使用して、派生クラスのプロパティに基づいて割り当てのGUIのラベルをそれに応じて設定しようとしています。

問題は、何らかの形で、リストクラスの格納された要素が特定の型として認識されるかどうかをプログラムがチェックしないことです。以下に、プログラムとクラスのための私のコードが含まれています。

List <>

private void comboBoxAccountNumber_SelectedIndexChanged(object sender, EventArgs e) 
    { 

     if (selectedBankAccount[0] is CheckingAccount) 
     { 
      labelOwnerID.Text = selectedBankAccount[0].AccountNumber; 
      labelBalance.Text = selectedBankAccount[0].Balance.ToString("c"); 

     } 

     else if (selectedBankAccount[1] is CheckingAccount) 
     { 
      labelOwnerID.Text = selectedBankAccount[1].AccountNumber; 
      labelBalance.Text = selectedBankAccount[1].Balance.ToString("c"); 

     } 


    } 

    List<BankAccount> selectedBankAccount = new List<BankAccount>(); 

    SavingsAccount savs1_Account; 
    CheckingAccount chek1_Account; 
    SavingsAccount savs2_Account; 
    CheckingAccount chek2_Account; 


    private void FormBankOfYourSelf_Load(object sender, EventArgs e) 
    { 
     savs1_Account = new SavingsAccount("0001", "31-1000", 100m, 0.01); 
     chek1_Account = new CheckingAccount("0001", "44-1000", 250m, true); 
     savs2_Account = new SavingsAccount("0002", "31-1001", 1000m, 0.0125); 
     chek2_Account = new CheckingAccount("0002", "44-1001", 500m, false); 
     selectedBankAccount.Add(chek1_Account); 
     selectedBankAccount.Add(chek2_Account); 

     comboBoxAccountNumber.Items.Add(selectedBankAccount[0].AccountNumber); 
     comboBoxAccountNumber.Items.Add(selectedBankAccount[1].AccountNumber); 


    } 



public abstract class BankAccount 
{ 
    // Fields - The data we want to store 
    // Naming convention for fields is to use underscore before the name 
    protected string _customerId; 
    protected string _accountNumber; 
    protected decimal _balance; 

    //Properties - Allow access to fields (Get/Set) 
    // Get = read access 
    // Set = write (modify) access 
    public string CustomerId 
    { 
     get { return _customerId; } 
     set { _customerId = value; } 
    } 

    public string AccountNumber 
    { 
     get { return _accountNumber; } 
     set { _accountNumber = value; } 
    } 

    public decimal Balance 
    { 
     get { return _balance; } 
    } 

    // Methods - The action or behaviors the class can do 
    // almost always, define the constructor and ToString methods 
    // Constructor creates (instantiates a new object) 
    public BankAccount(string customerId, string accountNumber, decimal initialBalance) 
    { 
     //fields are set = to the parameters(inputs from the form) 
     _customerId = customerId; 
     _accountNumber = accountNumber; 
     _balance = initialBalance; 
    } 

    public abstract bool Deposit(decimal depositAmount); 
    //{ 
    // // If the depositAmount is less then 0 then RETURN false 
    // if (depositAmount <= 0) 
    // { 
    //  return false; 
    // } 

    // // Otherwise, complete the deposit and RETURN true 
    // _balance += depositAmount; 
    // return true; 
    //} 

    public abstract bool Withdraw(decimal withdrawAmount); 
    //{ 
    // // If the withdrawAmount is greater than the balance or less then or equal to 0 
    // // then RETURN false (don't allow withdrawal) 
    // if (withdrawAmount > _balance || withdrawAmount <= 0) 
    // { 
    //  return false; 
    // } 

    // // Otherwise, complete the withdrawal and return true 
    // _balance -= withdrawAmount; 
    // return true; 
    //} 


    public class CheckingAccount : BankAccount 
    { 
    //private string _customerID; 
    //private string _accountNum; 
    //private decimal _initBalance; 
    private bool _overdraftProtection; 

    public CheckingAccount(string customerId, string accountNum, decimal initialBalance, bool overDraft) 
     :base(customerId, accountNum, initialBalance) 
    { 
     //_customerID = customerId; 
     //_accountNum = accountNum; 
     //_initBalance = initialBalance; 
     _overdraftProtection = overDraft; 
    } 

    public bool OverDraftProtection 
    { 
     get { return _overdraftProtection; } 

     set { _overdraftProtection = value; } 
    } 

    public override bool Deposit(decimal depositAmount) 
    { 
     if(depositAmount > 0) 
     { 
      _balance += depositAmount; 
      return true; 
     } 

     else 
     { 
      return false; 
     } 
    } 

    public override bool Withdraw(decimal withdrawAmount) 
    { 
     if(withdrawAmount <= _balance || withdrawAmount > 0) 
     { 
      _balance -= withdrawAmount; 
      return true; 
     } 

     else 
     { 
      return false; 
     } 
    } 




} 

**アップデート:私はコンボボックス内の別の項目を選択するにもかかわらず、私のプログラムを実行すると、それが値を変更していないことに気づきました選択項目に応じて以下は私のプログラムが実行され、異なる項目が選択されている状態です。

First selected item in combobox
Second selected item in combobox

+1

基本クラスのプロパティ( 'AccountNumber'と' Balance')のみを使用している場合、なぜ派生クラスが何であるか気にしますか? – Comintern

答えて

0

あなたのリストタイプBankAccountのように、プロパティが、その後isキーワードを使用して

利用可能になるとされます前に、あなたが仕事をしたい特定の継承されたクラスにオブジェクトをキャストする必要がありますasキーワードは、基本的に2回キャストされているため、高価なパフォーマンスが得られます。それは問題が判明投稿されたコメントからヌル

CheckingAccount account = selectedBankAccount[0] as CheckingAccount 
if(account != null) 
{ 
    //account is of type CheckingAccount so the additional 
    //CheckingAccount properties should be available via the account variable 
} 
+0

私は参照してください。うーん...リストクラスの要素を意図した継承クラスにキャストしようとしましたが、うまくいきません。プログラムを実行すると、コンボボックス内の別の項目を選択しても、選択項目に応じて値が変更されないことに気付きました。 –

0

ためasキーワードやテストを使用するのに最適なコードがコンボボックスで選択したBankAccountを選択されていないということです。 ComboBoxがどのように設定されているかを知らない。最も簡単な方法はcomboBoxAccountNumberにselectedBankAccountコレクションが設定されていると仮定して、選択したインデックスを使用することです。

private void comboBoxAccountNumber_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    BackAccount account = selectedBankAccount[comboBoxAccountNumber.SelectedIndex] 

    labelOwnerID.Text = account.AccountNumber; 
    labelBalance.Text = account.Balance.ToString("c"); 
} 
+0

はい、実際には意味があります。数時間前に、私はプログラムがコンボボックスの項目を知らないことを示すコードを読んでいないという問題を理解しました。手伝ってくれてどうもありがとう。私はあなたの方法を考える際に別の方法を行った。 –

関連する問題