簡體   English   中英

在C#中將Class初始化為變量

[英]Initialize of a Class as variable in c#

我聽不懂這段代碼。 為什么我需要將一個類初始化為變量,例如“ private InvoiceMaster _invoiceMaster”和“ InvoiceMaster im = new InvoiceMaster()”。 請詳細幫助我。 我需要非常清楚地了解。

  namespace AHS.Invoice.UI
 {
  public partial class ReRouteDetail : BasePage
  {
    #region Declaration
    private InvoiceMaster _invoiceMaster;
    DataSet _ds = new DataSet();
    InvoiceMasterCollection _invoiceMasterCollection = new InvoiceMasterCollection();

    #endregion

    #region Page Events
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        if (!IsPostBack)
        {

        }
        this.LoadColumns();
        this.LoadGridData();
    }
    #endregion  

    #region Methods


    private void LoadGridData()
    {

        if (base.CurrentScreen.ID == 3780)
        {
            _ds = new InvoiceMasterCollection().LoadReRouteData();
            gc.GridDataSource = _ds;
            gc.GridDataBind();
        }
        else if (base.CurrentScreen.ID == 3781)
        {
            _ds = new InvoiceMasterCollection().LoadReRouteFromServiceApproverData();
            gc.GridDataSource = _ds;
            gc.GridDataBind();
        }
        else if (base.CurrentScreen.ID == 3782)
        {
            _ds = new InvoiceMasterCollection().LoadReRouteFromServiceConfirmationData();
            gc.GridDataSource = _ds;
            gc.GridDataBind();
        }
    }

    #endregion


    #region Events
    protected void gc_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

                DropDownList ddlStatus = e.Row.Cells[7].FindControl("ddlStatus") as DropDownList;
                ddlStatus.CssClass = "ReRouteddlStatus";
                if (base.CurrentScreen.ID == 3780)
                {
                    DataSet reRouteDataSet = new InvoiceMasterCollection().LoadStatus(Convert.ToInt32(e.Row.Cells[4].Text));
                    ddlStatus.DataTextField = "Description";
                    ddlStatus.DataValueField = "ID";
                    ddlStatus.DataSource = reRouteDataSet;
                    ddlStatus.DataBind();
                }

                if (base.CurrentScreen.ID == 3781 || base.CurrentScreen.ID == 3782)
                {
                    ddlStatus.Enabled = false;                    
                }
            System.Web.UI.WebControls.Button btnReRoute = e.Row.Cells[8].FindControl("btnReRoute") as System.Web.UI.WebControls.Button;
            btnReRoute.CssClass = "btnBackToReRoute";
            //Button btnReRoute = e.Row.Cells[8].FindControl("btnReRoute") as Button;
            btnReRoute.CommandName = "ReRoute";
            btnReRoute.CommandArgument = e.Row.Cells[0].Text;
            e.Row.Cells[8].Controls.Add(btnReRoute);

        }
    }

    protected void gc_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "ReRoute")
        {
              int masterID = Convert.ToInt32(e.CommandArgument);

              Button button = null;
              foreach (GridViewRow rows in gc.GridRows)
              {
                  InvoiceMaster im = new InvoiceMaster();
                  im.ID = masterID;
                 // this.LoadGridData();

                  _invoiceMaster = new InvoiceMaster();
                  _invoiceMaster = im.GetData();

                  int id = Convert.ToInt32(rows.Cells[0].Text);
                  if (id == masterID)
                  {
                      button = rows.FindControl("btnReRoute") as Button;

   DropDownList ddlStatus(DropDownList)rows.Cells[6].FindControl("ddlStatus");
            if (base.CurrentScreen.ID == 3780)
                      {
                          _invoiceMaster.StatusID = Convert.ToInt32(ddlStatus.SelectedItem.Value);
                      }
                      if (base.CurrentScreen.ID == 3781)
                      {
                          _invoiceMaster.StatusID = 13;
                      }
                      if (base.CurrentScreen.ID == 3782)
                      {
                          _invoiceMaster.StatusID = 11;
                      }
                      _invoiceMaster.Save();

                      break;
                  }                     
              }

            LoadColumns();
            LoadGridData();
            base.ShowClientMessage("Invoice Backed Successfully.");
        }
    }

    #endregion
}

}

當您編寫此行時:

private InvoiceMaster _invoiceMaster;

您只是在定義InvoiceMaster類型的類的私有成員。 然而,在這一點上,引用沒有指向任何東西(“值”為null )。

在這一行:

InvoiceMaster im = new InvoiceMaster();

您還將創建該類的私有成員(c#中的默認成員是private ),這一次您將向該引用分配正在創建的新對象。

以下各行將無法編譯,並且必須在函數范圍內:

im.ID = masterID;

_invoiceMaster = new InvoiceMaster();
_invoiceMaster = im.GetData();

我建議您閱讀其中的許多教程之一,以更好地了解數據類型,變量和范圍

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM