我使用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;
}
答案 0 :(得分:0)
那么如何在EventCost中获取TotalCost中的内容?
如果EventCost
不是静态类,则应该有EventCost
类型的对象,我们称之为enentCost
。然后你可以将EventCost
作为
eventCost.TotalCost
否则,如果EventCost
是一个静态类,至少对我来说没有任何意义,你可以将EventCost
作为
EventCost.TotalCost
答案 1 :(得分:0)
好的,从你的代码我可以看到你需要一个Cost类的实例,在这里:
if/else
此外,您需要检查您的类命名空间是否可以从方法btnCalculate_Click访问,例如,类Cost在命名空间Project.Totals中,这里有一些代码:
public void btnCalculate_Click(object sender, EventArgs e)
{
var cost = new Cost();