C# 从另一个 class 更新 List[] 项
C# update List[] items from another class
我真的被这个问题困住了。我有两个 classes,Person 和 Expense。 Person 有 属性 MyName 和 List Expense 作为成员。在费用 class 中,它有 属性 MyFoodCost。我有一个表格,允许我 enter/update 我的 MyFoodCost 费用。在表单中,我有 List Person 因为会有更多的人。因此,如果我想更新特定人员的 MyFoodCost 费用,我该怎么做?这个特定的人会有 MyFoodCost 的新更新成本吗?
public class Expense
{
private decimal MyFoodCost;
public Expense(decimal food)
{
MyFoodCost = food;
}
public decimal FoodCost
{
set
{
MyFoodCost = value;
}
get
{
return MyFoodCost;
}
}
}
public class Person
{
private string MyName;
public List<Expense> MyExpense;
public Person(string name, decimal food)
{
MyName = name;
MyExpense = new List<Expense>();
MyExpense.Add(new Expense(food));
}
public string FullName
{
set
{
this.MyName = value;
}
get
{
return this.MyName;
}
}
}
public partial class BudgetForm : Form
{
public List<Person> person;
public BudgetForm()
{
InitializeComponent();
person = new List<Person>();
}
private void buttonAddExpense_Click(object sender, EventArgs e)
{
decimal food = 0;
food = decimal.Parse(TextBoxFood.Text);
string name = ComboBoxPerson.SelectedItem.ToString();
if(person.Count == 0)
{
person.Add(new Person(name, food));
}
else
{
Person you = person.FirstOrDefault(x => x.FullName == name);
if (you == null)
{
person.Add(new Person(name, food));
}
else
{
foreach (var item in person)
{
//check if person exists?
if (item.PersonName == name)
{
//person exists so update the food cost for him only.
//should i code to update the food
//or do somewhere else?
}
}
}
}
}
}
您已经找到 Person
对象。我认为您只想将新费用添加到该人的费用清单中。像这样:
Person you = person.First(x => x.FullName == name);
if (you == null)
{
person.Add(new Person(name, food));
}
else
{
you.MyExpense.Add(new Expense(food));
}
我真的被这个问题困住了。我有两个 classes,Person 和 Expense。 Person 有 属性 MyName 和 List Expense 作为成员。在费用 class 中,它有 属性 MyFoodCost。我有一个表格,允许我 enter/update 我的 MyFoodCost 费用。在表单中,我有 List Person 因为会有更多的人。因此,如果我想更新特定人员的 MyFoodCost 费用,我该怎么做?这个特定的人会有 MyFoodCost 的新更新成本吗?
public class Expense
{
private decimal MyFoodCost;
public Expense(decimal food)
{
MyFoodCost = food;
}
public decimal FoodCost
{
set
{
MyFoodCost = value;
}
get
{
return MyFoodCost;
}
}
}
public class Person
{
private string MyName;
public List<Expense> MyExpense;
public Person(string name, decimal food)
{
MyName = name;
MyExpense = new List<Expense>();
MyExpense.Add(new Expense(food));
}
public string FullName
{
set
{
this.MyName = value;
}
get
{
return this.MyName;
}
}
}
public partial class BudgetForm : Form
{
public List<Person> person;
public BudgetForm()
{
InitializeComponent();
person = new List<Person>();
}
private void buttonAddExpense_Click(object sender, EventArgs e)
{
decimal food = 0;
food = decimal.Parse(TextBoxFood.Text);
string name = ComboBoxPerson.SelectedItem.ToString();
if(person.Count == 0)
{
person.Add(new Person(name, food));
}
else
{
Person you = person.FirstOrDefault(x => x.FullName == name);
if (you == null)
{
person.Add(new Person(name, food));
}
else
{
foreach (var item in person)
{
//check if person exists?
if (item.PersonName == name)
{
//person exists so update the food cost for him only.
//should i code to update the food
//or do somewhere else?
}
}
}
}
}
}
您已经找到 Person
对象。我认为您只想将新费用添加到该人的费用清单中。像这样:
Person you = person.First(x => x.FullName == name);
if (you == null)
{
person.Add(new Person(name, food));
}
else
{
you.MyExpense.Add(new Expense(food));
}