我正在從這個程序開始工作,但我不知道問題的原因。當我運行程序並輸入從檢查賬戶轉移到儲蓄賬戶的金額時,程序會從檢查賬戶中扣除輸入的金額,但不會添加到儲蓄賬戶。從支票賬戶轉賬?
我該如何解決這個問題?任何幫助表示讚賞。
public partial class Transfer : Window
{
private string PIN;
Accounts AccountsList = new Accounts();
//constructor
public Transfer(string pin, Accounts myAcounts)
{
InitializeComponent();
AccountsList = myAcounts;
PIN = pin;
}
//save to file method
public void saveToFile()
{
using (StreamWriter sw = new StreamWriter("Acounts.txt"))
{
for (int i = 0; i < AccountsList.Count; i++)
{
var data = new List<string>
{
AccountsList[i].ACCOUNTYPE.ToString()
,AccountsList[i].PIN
,AccountsList[i].ACCOUNTNUMBER
,AccountsList[i].ACCOUNTBALANCE.ToString()
};
var account = String.Join(";", data);
sw.WriteLine(account);
}
}
}
private void btnOK_Click(object sender, RoutedEventArgs e)
{
try
{
string txtAmount = txtAmountInTransfer.Text;
double amount = 0;
bool AmountCorrect = double.TryParse(txtAmount, out amount);
Account chequingAccount = new Account();
Account savingAccount = new Account();
//deposit from CHEQUING ACCOUNT to SAVING ACCOUNT
{
//validate user entries
for (int i = 0; i < AccountsList.Count; i++)
{
//withdraw from CHEQUING ACCOUNG
if (AccountsList[i].ACCOUNTYPE == 'C' && AccountsList[i].PIN == PIN)
{
if (rbChequing_to_Saving.IsChecked == true)
{
chequingAccount = AccountsList[i];
}
else
{
savingAccount = AccountsList[i];
}
chequingAccount.ACCOUNTBALANCE -= amount;
AccountsList[i].ACCOUNTBALANCE += amount;
//saveToFile();
//break;
}
//if (AccountsList[i].ACCOUNTYPE == 'S')
// savingAccount.ACCOUNTBALANCE += amount;
//saveToFile();
}
}
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
}
}
}
你能解釋一下我一點請即時通訊新的C# – afgboy
@afgboy:由於賬戶(類)是引用類型,當你做'chequingAccount = AccountsList [I]',您要複製AccountsList的'參考文獻[1 ]'到'chequingAccount'。所以如果你改變其中的任何一個,那會影響另一個的價值。閱讀**值類型與參考類型**。 – DarkKnight