我无法理解这段代码。为什么我需要将类初始化为变量如“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
}
}
答案 0 :(得分:1)
当你写这一行时:
private InvoiceMaster _invoiceMaster;
您只是定义类型为InvoiceMaster
的类的私有成员。此时,引用指向任何内容(“值”为null
)。
在这一行:
InvoiceMaster im = new InvoiceMaster();
您还要创建该类的私有成员(c#中的默认值为private
),这次您将为该引用分配您正在创建的新对象。
以下行不会编译,必须在函数范围内:
im.ID = masterID;
_invoiceMaster = new InvoiceMaster();
_invoiceMaster = im.GetData();
我建议你通过其中的一个教程来更好地理解数据类型,变量和范围