0

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
}

}

1
  • That code wouldn't compile at the moment, as you have statements at the class level where there should only be declarations. If you don't know why it's not valid, that would be a useful question to ask. If that's not actually the code you're interested in, please provide a minimal reproducible example instead. Commented Oct 5, 2016 at 10:53

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

Yes. I get this code in a function. But I was not clear about these "private InvoiceMaster _invoiceMaster" And "InvoiceMaster im = new InvoiceMaster() " two line. Your answer is clear to me. But If Use.InvoiceMaster im = new InvoiceMaster(); im.ID = masterID; im.GetData(); its showing null. But when i use _invoiceMaster = new InvoiceMaster(); _invoiceMaster = im.GetData(); it;s showing record. Why?
@user3510330 - please edit your question and show a more complete version of your code
I added my complete version of code . Please help to understand.
@user3510330 - Where exactly is the problem?.. Line of code
there is no error here. But if both of the declaration are private member of a class. Why i need to create "InvoiceMaster im = new InvoiceMaster()" for assigning a object. Why isnt it _invoiceMaster.ID = masterID Instead of im.ID = masterID;. I tried by this code but It's showing null exception.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.