无法重用类的Object

时间:2014-11-19 00:35:15

标签: 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 : 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类的新实例。

理想情况下,我希望整个页面只有一个对象,并在需要时重复使用它。

2 个答案:

答案 0 :(得分:3)

阅读您的代码,它不会对性能产生任何影响。每次请求都会创建您的页面(实例化_Default类的对象)。

有一些可能性:

  1. 在类级别将您的方法声明为私有或受保护。但是,当您仅在Click方法中使用此对象时,每个请求都有一个BankAccount对象,您已经拥有该对象。 (不会提高性能,也不会减少内存使用量)
  2. 您可以将该字段声明为静态,但您的网站上的所有用户都拥有相同的对象(如果同时执行了两个请求,您不希望有人看到错误的余额)。< / LI>
  3. 您可以将银行帐户对象存储在会话中并使用它,如果您不想每次都创建它。 (会增加内存使用量,你也会使用性能,因为对象被序列化,反序列化并且必须执行强制转换)
  4. 你为什么要这样做?性能不会更好,内存使用率也不会优化。

答案 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;