C#将活动添加到另一个列表 [英] C# add actives to another list
问题描述
您好我有一个名为Animal.cs的课程,在我的Program.cs中我创造了一些动物..
Hello i have one class called Animal.cs and in my Program.cs i have created some animals..
class Program
{
static void Main(string[] args)
{
var AnimalList = new AnimslList();
animalList.Add(new Animal{ Active = true, Name = "Bob", Photo = "Espanha" });
animalList.Add(new Animal { Active = false, Name = "Robby", Photo = "Portugal" });
animalList.Add(new Animal { Active = true, Name = "Snoop", Photo = "UK" });
}
}
我有一个类AnimalList.cs并且在那个类中我添加了动物列表,我正在用这个获取活跃的动物代码:
and i have one class AnimalList.cs and in that class i added Animal list, and im getting the active animals with this code:
class AnimalList : List<Animal>
{
public List<Animal> GetActiveAnimals()
{
return this.FindAll(p => p.Active);
}
}
我的问题是在这个类中他在event.cs中调用了Event.cs我希望得到GetActiveAnimals()的结果因为我只想得到活动的动物,有人可以帮我吗?
and my problem is in this class he called Event.cs in event.cs i want get the results from GetActiveAnimals() because i just want to get the active animals for the event can someone help me please?
class Event
{
private AnimalList _contestants;
}
我应该从上面这个中的AnimaList获得Active。活跃的应该进入_contestants
我尝试过:
我不知道如何真正需要帮助这是我的代码的最后一个问题我认为
i nee来获取列表中此类的活动_contestants
class活动
{
私人AnimalList _contestants;
}
i should get the Active from the AnimaList in this one above. The active ones should go into _contestants
What I have tried:
I didn't know how to i really need help with this it's my last problem of this code i think
i nee to get the actives in this class in the list _contestants
class Event
{
private AnimalList _contestants;
}
推荐答案
问题是你的Main方法中的AnimalList:
The problem is that your AnimalList in your Main method:
static void Main(string[] args)
{
var AnimalList = new AnimslList();
animalList.Add(new Animal{ Active = true, Name = "Bob", Photo = "Espanha" });
animalList.Add(new Animal { Active = false, Name = "Robby", Photo = "Portugal" });
animalList.Add(new Animal { Active = true, Name = "Snoop", Photo = "UK" });
}
函数的本地函数 - 你不能在函数外部访问它,除非你把它传递给另一个方法。
所以如果你这样做的话:
Is local to the function - you can't access it outside the function unless you pass it to another method.
So if you were to do this:
static void Main(string[] args)
{
var AnimalList = new AnimslList();
animalList.Add(new Animal{ Active = true, Name = "Bob", Photo = "Espanha" });
animalList.Add(new Animal { Active = false, Name = "Robby", Photo = "Portugal" });
animalList.Add(new Animal { Active = true, Name = "Snoop", Photo = "UK" });
Event ev = new Event();
ev.ListOfAnimalsPropertyYouNeedToAdd = AnimalList;
...
}
然后,您可以使用您传入的列表来识别活动列表:
You can then use the list you passed in to identify the active ones:
foreach (Animal a in ev.GetActiveAnimals())
{
...
}
通过在Event类中编写代码:
By writing the code in your Event class:
public List<Animal> GetActiveAnimals()
{
return ListOfAnimalsYouSavedWhenYouSetThePropertyEarlier.GetActiveAnimals();
}
(但不要称之为活动
- 在C#中具有特定含义,您的老师将在稍后解释)
(But don't call it Event
- that has a specific meaning in C# that your teacher will explain later)
这篇关于C#将活动添加到另一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!