[英]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()". 为什么我需要将一个类初始化为变量,例如“ private InvoiceMaster _invoiceMaster”和“ 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
. 您只是在定义InvoiceMaster
类型的类的私有成员。 At this point however the reference is pointing to nothing (the "value" is null
). 然而,在这一点上,引用没有指向任何东西(“值”为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. 您还将创建该类的私有成员(c#中的默认成员是private
),这一次您将向该引用分配正在创建的新对象。
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 我建议您阅读其中的许多教程之一,以更好地了解数据类型,变量和范围
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.