我正在尝试使用C#中的面向对象设计创建页面。我想在我的页面中一直使用相同的实例对象,但它无法正常工作。
以下是我的代码:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlDataSource1.Delete();
}
}
protected void ViewBalance_Click(object sender, EventArgs e)
{
string tempString = Request.Form["BalanaceField"];
double bal;
Double.TryParse(tempString, out bal);
Session["CurrentBalance"] = bal;
BankAccount newAcc = new BankAccount(bal);
resultDiv.InnerHtml = "<h1> Current Balance is $" +
newAcc.getCurrentBalance() + "</h1>";
transactionDiv.Style["display"] = "block";
newAccountDiv.Style["display"] = "none";
}
protected void Withdraw_Click(object sender, EventArgs e)
{
string currentBal = Session["CurrentBalance"].ToString();
double bal;
Double.TryParse(currentBal, out bal);
BankAccount newAcc = new BankAccount(bal);
double withdrwaAmount;
Double.TryParse(Request.Form["WithdrawField"], out withdrwaAmount);
if (newAcc.validWithDraw(withdrwaAmount))
{
newAcc.withDraw(withdrwaAmount);
Session["CurrentBalance"] = newAcc.getCurrentBalance();
insertRecord("Withdaw", withdrwaAmount, newAcc.getCurrentBalance());
resultDiv.InnerHtml =
"<h1>Amount Withdrwan Succesfully. Current Balance is $ " +
newAcc.getCurrentBalance() + "</h1>";
}
else
{
resultDiv.InnerHtml =
"<h1>You cann't ovewdraw you account. Your current Balance is $" +
bal + " and you are trying to widthdraw $" +
withdrwaAmount + " </h1>";
}
}
protected void Deposit_Click(object sender, EventArgs e)
{
string currentBal = Session["CurrentBalance"].ToString();
double bal;
Double.TryParse(currentBal, out bal);
BankAccount newAcc = new BankAccount(bal);
double depositAmount;
Double.TryParse(Request.Form["DeopositField"], out depositAmount);
double newBalance = newAcc.deposit(depositAmount);
Session["CurrentBalance"] = newBalance;
insertRecord("Deposit", depositAmount, newAcc.getCurrentBalance());
resultDiv.InnerHtml =
"<h1>Amount Deposit Succesfully. Current Balance is $ " +
newAcc.getCurrentBalance() + "</h1>";
}
protected void InterestCount_Click(object sender, EventArgs e)
{
string currentBal = Session["CurrentBalance"].ToString();
double bal;
Double.TryParse(currentBal, out bal);
BankAccount newAcc = new BankAccount(bal);
double interestMonths;
Double.TryParse(Request.Form["MonthsField"], out interestMonths);
double interest = bal * (0.10) * (interestMonths / 12);
resultDiv.InnerHtml =
"<h1>Total amount with acculmated Interest would be $ " +
(newAcc.getCurrentBalance() + interest) +
" and Interest would be $:" + interest + "</h1>";
}
public void insertRecord(string type, double amount, double finalAMount)
{
DataView dv =
(DataView) SqlDataSource1.Select(DataSourceSelectArguments.Empty);
int id = dv.Count + 1;
SqlDataSource1.InsertParameters.Add("Id", id.ToString());
SqlDataSource1.InsertParameters.Add("Type", type);
SqlDataSource1.InsertParameters.Add("Amount", amount.ToString());
SqlDataSource1.InsertParameters.Add("Balance", finalAMount.ToString());
SqlDataSource1.Insert();
}
}
正如我们所看到的,我必须始终在每个方法中创建BankAccount类的新实例。
理想情况下,我希望整个页面只有一个对象,并在需要时重复使用它。
答案 0 :(得分:3)
阅读您的代码,它不会对性能产生任何影响。每次请求都会创建您的页面(实例化_Default类的对象)。
有一些可能性:
你为什么要这样做?性能不会更好,内存使用率也不会优化。
答案 1 :(得分:1)
将BankAccount newAcc
的定义放在protected void Page_Load(object sender, EventArgs e)
之上(对&#34;范围&#34进行一些研究,以了解其工作原理)
在Page_Load
内创建您的第一个实例
newAcc = new BankAccount(); //note that you'll want a constructor that doesn't have a balance argument
确保您有一种公开的方式来修改balance
字段,然后当您需要更改余额时:
newAcc.balance = whatever;