简体   繁体   中英

Initialize of a Class as variable in c#

I couldn't understand this code. Why i need to initialize a class as variable Like "private InvoiceMaster _invoiceMaster" And "InvoiceMaster im = new InvoiceMaster()". Please Help me in details. I need to very clear understand.

  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
}

}

When you write this line:

private InvoiceMaster _invoiceMaster;

you are just defining a private member of the class of type InvoiceMaster . At this point however the reference is pointing to nothing (the "value" is null ).

In this line:

InvoiceMaster im = new InvoiceMaster();

you are also creating a private member of the class (the default in c# is private ) and this time you are assigning to that reference a new object that you are creating.

The following lines will not compile and must be in a scope of a function:

im.ID = masterID;

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

I recommend that you go through one of the many tutorial out there to better understand about data types, variables and scopes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM