试图缩短我的程序,以便不重复代码

时间:2013-12-20 16:17:46

标签: c#

我正在编写一个程序,使用可观察的集合将膳食类型过滤到不同的类别。我正在使用枚举对膳食进行分类,并且我有三个单独的方法使用相同的代码,在单击各自的按钮时将它们分成新的集合。三种枚举类型是素食,肉类和鱼类。我有两个可观察的收藏品,餐和过滤餐。我试图创建另一种方法,然后传递类别作为参数,但我无法让它工作!任何帮助将不胜感激。

private void btnVegetarian_Click(object sender, RoutedEventArgs e)
    {
        filteredMeals = new ObservableCollection<Meal>();
        Meal newMeal = new Meal();

        for (int i = 0; i < meals.Count; i++)
        {
            newMeal = meals[i];
            if (newMeal.Category == MealCategory.Vegetarian)
            {
                filteredMeals.Add(newMeal);
            }
        }
        lbxMeals.ItemsSource = filteredMeals;
    }

    private void btnMeat_Click(object sender, RoutedEventArgs e)
    {
        filteredMeals = new ObservableCollection<Meal>();
        Meal newMeal = new Meal();

        for (int i = 0; i < meals.Count; i++)
        {
            newMeal = meals[i];
            if (newMeal.Category == MealCategory.Meat)
            {
                filteredMeals.Add(newMeal);
            }
        }
        lbxMeals.ItemsSource = filteredMeals;

    }

    private void btnFish_Click(object sender, RoutedEventArgs e)
    {
       filteredMeals = new ObservableCollection<Meal>();
        Meal newMeal = new Meal();

        for (int i = 0; i < meals.Count; i++)
        {
            newMeal = meals[i];
            if (newMeal.Category == MealCategory.Fish)
            {
                filteredMeals.Add(newMeal);
            }
        }
        lbxMeals.ItemsSource = filteredMeals;
    }

3 个答案:

答案 0 :(得分:7)

您需要创建一个采用MealCategory参数的新方法。将代码移到那里,并为每个按钮点击处理程序传递适当的MealCategory。

代码可能如下所示:

private void btnVegetarian_Click(object sender, RoutedEventArgs e)
{
    FilterMeals(MealCategory.Vegatarian);
}

private void btnMeat_Click(object sender, RoutedEventArgs e)
{
    FilterMeals(MealCategory.Meat);
}

private void btnFish_Click(object sender, RoutedEventArgs e)
{
   FilterMeals(MealCategory.Fish);
}

private void FilterMeals(MealCategory category)
{
    filteredMeals = new ObservableCollection<Meal>();
    Meal newMeal = new Meal();

    for (int i = 0; i < meals.Count; i++)
    {
        newMeal = meals[i];
        if (newMeal.Category == category)
        {
            filteredMeals.Add(newMeal);
        }
    }
    lbxMeals.ItemsSource = filteredMeals;
}

一旦你有了这个工作,你可以尝试重构你的FilterMeals方法。您可以使用LINQ来表示过滤器,并且ObservableCollection构造函数具有带IEnumerable<T>的重载,这可以将其简化为:

private void FilterMeals(MealCategory category)
{
    var filteredMeals = meals.Where(m => m.Category == category);
    lbxMeals.ItemsSource = new ObservableCollection<Meal>(filteredMeals);
}

答案 1 :(得分:0)

private void btnVegetarian_Click(object sender, RoutedEventArgs e)
{
    Filer(MealCategory.Vegatarian).Invoke();
}

private void btnMeat_Click(object sender, RoutedEventArgs e)
{
    Filer(MealCategory.Meat).Invoke();
}

private void btnFish_Click(object sender, RoutedEventArgs e)
{
   Filer(MealCategory.Fish).Invoke();
}
public Action Filer(MealCategory mealCategory)
 {
    lbxMeals.ItemsSource = new ObservableCollection<Meal>(meals.Where(m=>m.Category=mealCategory))
  }

答案 2 :(得分:0)

太麻烦了。你可以这样做:

private void btnMeat_Click(object sender, RoutedEventArgs e)
{
    lbxMeals.ItemsSource = new ObservableCollection<Meal>(
                              meals.Where(m => m.Category == MealCategory.Meat));
}

当然素食和鱼类也一样。