C# 无法重用类的对象
我正在尝试使用C#中的面向对象设计创建一个页面。我想在我的页面中一直使用相同的实例对象,但它不起作用 下面是我的代码:C# 无法重用类的对象,c#,asp.net,object,C#,Asp.net,Object,我正在尝试使用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 :
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();
}
}
使用系统;
使用System.Collections.Generic;
使用系统数据;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
公共部分类\u默认值:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
SqlDataSource1.Delete();
}
}
受保护的void ViewBalance\u单击(对象发送者,事件参数e)
{
string tempString=Request.Form[“BalanaceField”];
双bal;
双锥虫(tempString,out bal);
会话[“当前余额”]=余额;
BankAccount newAcc=新银行账户(bal);
resultDiv.InnerHtml=“当前余额为$”+
newAcc.getCurrentBalance()+“”;
transactionDiv.Style[“显示”]=“块”;
newAccountDiv.Style[“显示”]=“无”;
}
受保护的无效撤消\u单击(对象发送方,事件参数e)
{
字符串currentBal=会话[“CurrentBalance”]。ToString();
双bal;
双锥虫(当前余额,外余额);
BankAccount newAcc=新银行账户(bal);
金额加倍;
Double.TryParse(请求表格[“取款字段”],不含DRWAAmount);
if(新账户有效期(含提款金额))
{
新账户提款(含DRWA金额);
会话[“CurrentBalance”]=newAcc.getCurrentBalance();
insertRecord(“Withdaw”,withdrwaAmount,newAcc.getCurrentBalance());
resultDiv.InnerHtml=
“成功使用DRWAN的金额。当前余额为美元”+
newAcc.getCurrentBalance()+“”;
}
其他的
{
resultDiv.InnerHtml=
“您不能注销您的帐户。您当前的余额为$”+
bal+“您正在尝试绘制$”+
withdrwaAmount+“”;
}
}
受保护的无效存款\u单击(对象发送方,事件参数e)
{
字符串currentBal=会话[“CurrentBalance”]。ToString();
双bal;
双锥虫(当前余额,外余额);
BankAccount newAcc=新银行账户(bal);
双倍存款额;
Double.TryParse(请求表[“DeospositField”]、外存金额);
双新余额=新账户存款(存款金额);
会话[“当前余额”]=新余额;
插入记录(“存款”,存款金额,新账户getCurrentBalance());
resultDiv.InnerHtml=
“已成功存入金额。当前余额为美元”+
newAcc.getCurrentBalance()+“”;
}
受保护的void InterestCount\u单击(对象发送方,事件参数e)
{
字符串currentBal=会话[“CurrentBalance”]。ToString();
双bal;
双锥虫(当前余额,外余额);
BankAccount newAcc=新银行账户(bal);
双利月;
Double.TryParse(请求表[“MonthsField”]、外利息月);
双倍利息=余额*(0.10)*(利息月/12);
resultDiv.InnerHtml=
“累计利息总额为美元”+
(新会计科目getCurrentBalance()+利息)+
“利息是美元:“+利息+”;
}
公共void insertRecord(字符串类型、双倍金额、双倍最终金额)
{
数据视图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());
添加(“Balance”,finalAMount.ToString());
SqlDataSource1.Insert();
}
}
正如我们所看到的,我必须始终在每个方法中创建BankAccount类的新实例
理想情况下,我希望整个页面只使用一个对象,并在需要时重用它。将您对
银行账户新ACC的定义放置在受保护的无效页面加载(对象发送方,事件参数e)
的正上方(对“范围”进行一些研究,以了解其工作原理)
在页面加载中创建您的第一个实例
newAcc = new BankAccount(); //note that you'll want a constructor that doesn't have a balance argument
确保您有修改余额
字段的公共方式,然后在需要更改余额时:
newAcc.balance = whatever;
将您对BankAccount newAcc
的定义放在受保护的无效页面加载(对象发送者,事件参数e)
的正上方(对“范围”进行一些研究,以了解其工作原理)
在页面加载中创建您的第一个实例
newAcc = new BankAccount(); //note that you'll want a constructor that doesn't have a balance argument
确保您有修改余额
字段的公共方式,然后在需要更改余额时:
newAcc.balance = whatever;
阅读代码不会对性能产生任何影响。每个请求都会创建页面(实例化一个默认类的对象)
有一些可能性:
在类级别上声明方法上方的private或protected字段。但是,由于您仅在单击方法中使用此对象,因此每个请求将有一个BankAccount对象,而您已经有了这个对象。(不会提高性能,也不会减少内存使用)
您可以将字段声明为static,但是您的网站上的所有用户都有相同的对象(如果同时执行两个请求,您不希望有人看到错误的平衡)
如果不想每次都创建银行帐户对象,可以将其存储在会话中并使用它。(将增加内存使用,您还将使用性能,因为对象是序列化、反序列化的,并且必须执行强制转换)
你为什么要这样做?性能不会更好,内存使用也不会得到优化。读取代码也不会