名称';总成本';当前上下文中不存在

本文关键字:不存在 上下文 总成本 名称 | 更新日期: 2023-09-27 18:25:38

我用Return 的新实例创建了Return类

但我收到一个错误,说Totalcost在当前的中不存在

变量TotalCost包含在另一个称为Cost 的类中

那么,我如何将Cost类中TotalCost中的内容放入Return类中,以便与输出一起使用呢

public void btnCalculate_Click(object sender, EventArgs e) 
{
    //Write all details to a text file that is created on the desktop
    Return newTextFile = new Return();   
    {
        txtTotalCost.Text = ("£" + TotalCost.ToString());
    }
    newTextFile.EventDetailsFile = "C:''AllUsers''User''Desktop''EventDetails.txt";
    System.IO.StreamWriter objWriter;
    objWriter = new System.IO.StreamWriter(newTextFile.EventDetailsFile);
    objWriter.Write("Event Type:" + EventList.SelectedItem + "'n");
    objWriter.Write("Event Name:" + txtEventName.Text + "'n");
    objWriter.Write("Customer Name:" + txtName.Text + "'n");
    objWriter.Write("Customer Address:" + txtAddress.Text + "'n");
    objWriter.Write("Event Start Date:" +txtStart.Text + "'n");
    objWriter.Write("Event End Date:" + txtEnd.Text + "'n");
    objWriter.Write("Number of People Attending:" + txtPeople.Text + "'n");
    objWriter.Write("Total Event Cost: £" + TotalCost);
    objWriter.Close();
    MessageBox.Show("The text file has been Created");
}

类成本中的代码如下

class Cost
{
    public int TotalCost;
    public int Event;
    public int personalEvent;
    public int organisationalEvent;
}

名称';总成本';当前上下文中不存在

那么,如何在EventCost中获取TotalCost中包含的内容呢?

如果EventCost不是一个静态类,那么应该有一个类型为EventCost的对象,我们称之为enentCost。然后你就可以把EventCost作为

eventCost.TotalCost

否则,如果EventCost是一个静态类,至少对我来说没有任何意义,那么你可以将EventCost作为

EventCost.TotalCost

好的,从您的代码中,我可以看到您需要一个类Cost的实例,在这里:

public void btnCalculate_Click(object sender, EventArgs e) 
{
    var cost = new Cost();

此外,你需要检查你的类命名空间是否可以从方法btnCalculate_Click访问,例如类Cost在命名空间Project.Totals中,这里有一些代码:

using Project.Totals;
public void btnCalculate_Click(object sender, EventArgs e) 
{
    var cost = new Cost();